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

对话框与窗口

开发平台:

Visual C++

  1. // SmartLayoutView.cpp : implementation of the CSmartLayoutView class
  2. //
  3. #include "stdafx.h"
  4. #include "SmartLayout.h"
  5. #include "SmartLayoutDoc.h"
  6. #include "CntrItem.h"
  7. #include "SmartLayoutView.h"
  8. #include "ControlSelector.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CSmartLayoutView
  16. IMPLEMENT_DYNCREATE(CSmartLayoutView, CRichEditView)
  17. BEGIN_MESSAGE_MAP(CSmartLayoutView, CRichEditView)
  18. //{{AFX_MSG_MAP(CSmartLayoutView)
  19. // NOTE - the ClassWizard will add and remove mapping macros here.
  20. //    DO NOT EDIT what you see in these blocks of generated code!
  21. ON_WM_DESTROY()
  22. //}}AFX_MSG_MAP
  23. // Standard printing commands
  24. ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
  25. ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
  26. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
  27. ON_COMMAND(ID_CHAR_BOLD, OnCharBold)
  28. ON_UPDATE_COMMAND_UI(ID_CHAR_BOLD, OnUpdateCharBold)
  29. ON_COMMAND(ID_CHAR_ITALIC, OnCharItalic)
  30. ON_UPDATE_COMMAND_UI(ID_CHAR_ITALIC, OnUpdateCharItalic)
  31. ON_COMMAND(ID_CHAR_UNDERLINE, OnCharUnderline)
  32. ON_UPDATE_COMMAND_UI(ID_CHAR_UNDERLINE, OnUpdateCharUnderline)
  33. ON_COMMAND(ID_PARA_CENTER, OnParaCenter)
  34. ON_UPDATE_COMMAND_UI(ID_PARA_CENTER, OnUpdateParaCenter)
  35. ON_COMMAND(ID_PARA_LEFT, OnParaLeft)
  36. ON_UPDATE_COMMAND_UI(ID_PARA_LEFT, OnUpdateParaLeft)
  37. ON_COMMAND(ID_PARA_RIGHT, OnParaRight)
  38. ON_UPDATE_COMMAND_UI(ID_PARA_RIGHT, OnUpdateParaRight)
  39. ON_COMMAND(ID_INSERT_BULLET, CRichEditView::OnBullet)
  40. ON_UPDATE_COMMAND_UI(ID_INSERT_BULLET, CRichEditView::OnUpdateBullet)
  41. ON_NOTIFY(XTP_FN_GETFORMAT, ID_COMBO_SIZE, OnGetCharFormat)
  42. ON_NOTIFY(XTP_FN_SETFORMAT, ID_COMBO_SIZE, OnSetCharFormat)
  43. ON_UPDATE_COMMAND_UI(ID_COMBO_SIZE, OnUpdateComboSize)
  44. ON_NOTIFY(XTP_FN_SETFORMAT, ID_COMBO_FONT, OnSetCharFormat)
  45. ON_UPDATE_COMMAND_UI(ID_COMBO_FONT, OnUpdateComboFont)
  46. ON_XTP_EXECUTE(ID_SELECTOR_TEXT, OnSelectorText)
  47. ON_UPDATE_COMMAND_UI(ID_SELECTOR_TEXT, OnUpdateSelectorText)
  48. ON_COMMAND(ID_BUTTON_TEXT, OnButtonText)
  49. ON_UPDATE_COMMAND_UI(ID_BUTTON_TEXT, OnUpdateText)
  50. ON_COMMAND(XTP_IDS_AUTOMATIC, OnTextAuto)
  51. ON_UPDATE_COMMAND_UI(XTP_IDS_AUTOMATIC, OnUpdateTextAuto)
  52. ON_COMMAND(XTP_IDS_MORE_COLORS, OnTextMore)
  53. ON_XTP_EXECUTE(ID_EDIT_UNDO, OnEditUndo)
  54. ON_NOTIFY(XTP_LBN_SELCHANGE, ID_EDIT_UNDO, OnListBoxControlSelChange)
  55. ON_NOTIFY(XTP_LBN_POPUP, ID_EDIT_UNDO, OnListBoxControlPoup)
  56. ON_NOTIFY(XTP_SLN_SELCHANGE, ID_INSERT_COLUMNS, OnColumnsSelChange)
  57. ON_NOTIFY(XTP_SLN_SELCHANGE, ID_INSERT_TABLE, OnTableSelChange)
  58. ON_XTP_EXECUTE(ID_INSERT_TABLE, OnInsertTable)
  59. ON_XTP_EXECUTE(ID_INSERT_COLUMNS, OnInsertColumns)
  60. ON_UPDATE_COMMAND_UI(ID_INSERT_TABLE, OnEnableCommand)
  61. ON_UPDATE_COMMAND_UI(ID_INSERT_COLUMNS, OnEnableCommand)
  62. ON_COMMAND_RANGE(ID_BDR_OUTSIDE, ID_BDR_INSIDE_VERT, OnBorders)
  63. ON_UPDATE_COMMAND_UI_RANGE(ID_BDR_OUTSIDE, ID_BDR_INSIDE_VERT, OnUpdateBorders)
  64. END_MESSAGE_MAP()
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CSmartLayoutView construction/destruction
  67. CSmartLayoutView::CSmartLayoutView()
  68. {
  69. m_clr = 0;
  70. m_dwBorders = 0;
  71. }
  72. CSmartLayoutView::~CSmartLayoutView()
  73. {
  74. }
  75. BOOL CSmartLayoutView::PreCreateWindow(CREATESTRUCT& cs)
  76. {
  77. // TODO: Modify the Window class or styles here by modifying
  78. //  the CREATESTRUCT cs
  79. return CRichEditView::PreCreateWindow(cs);
  80. }
  81. void CSmartLayoutView::OnInitialUpdate()
  82. {
  83. CRichEditView::OnInitialUpdate();
  84. // Set the printing margins (720 twips = 1/2 inch).
  85. SetMargins(CRect(720, 720, 720, 720));
  86. }
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CSmartLayoutView printing
  89. BOOL CSmartLayoutView::OnPreparePrinting(CPrintInfo* pInfo)
  90. {
  91. // default preparation
  92. return DoPreparePrinting(pInfo);
  93. }
  94. void CSmartLayoutView::OnDestroy()
  95. {
  96. CRichEditView::OnDestroy();
  97. }
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CSmartLayoutView diagnostics
  100. #ifdef _DEBUG
  101. void CSmartLayoutView::AssertValid() const
  102. {
  103. CRichEditView::AssertValid();
  104. }
  105. void CSmartLayoutView::Dump(CDumpContext& dc) const
  106. {
  107. CRichEditView::Dump(dc);
  108. }
  109. CSmartLayoutDoc* CSmartLayoutView::GetDocument() // non-debug version is inline
  110. {
  111. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSmartLayoutDoc)));
  112. return (CSmartLayoutDoc*)m_pDocument;
  113. }
  114. #endif //_DEBUG
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CSmartLayoutView message handlers
  117. void CSmartLayoutView::OnGetCharFormat(NMHDR* pNMHDR, LRESULT* pRes)
  118. {
  119. ASSERT(pNMHDR != NULL);
  120. ASSERT(pRes != NULL);
  121. ((NMXTPCHARHDR*)pNMHDR)->cf = GetCharFormatSelection();
  122. *pRes = 1;
  123. }
  124. void CSmartLayoutView::OnSetCharFormat(NMHDR* pNMHDR, LRESULT* pRes)
  125. {
  126. ASSERT(pNMHDR != NULL);
  127. ASSERT(pRes != NULL);
  128. #if (_MSC_VER < 1300)
  129. CHARFORMAT& cf = ((NMXTPCHARHDR*)pNMHDR)->cf;
  130. #else
  131. CHARFORMAT2 cf;
  132. MEMCPY_S(&cf, &((NMXTPCHARHDR*)pNMHDR)->cf, sizeof(((NMXTPCHARHDR*)pNMHDR)->cf));
  133. #endif
  134. SetCharFormat(cf);
  135. *pRes = 1;
  136. }
  137. void CSmartLayoutView::OnUpdateComboFont(CCmdUI* pCmd)
  138. {
  139. CXTPCommandBar* pToolBar = (CXTPToolBar*)pCmd->m_pOther;
  140. if (pToolBar)
  141. {
  142. CXTPControlFontComboBox* pFontCombo = (CXTPControlFontComboBox*)pToolBar->GetControls()->GetAt(pCmd->m_nIndex);
  143. if (pFontCombo->GetType() == xtpControlComboBox)
  144. {
  145. pFontCombo->SetCharFormat((CHARFORMAT&)GetCharFormatSelection());
  146. }
  147. }
  148. pCmd->Enable(TRUE);
  149. }
  150. void CSmartLayoutView::OnUpdateComboSize(CCmdUI* pCmd)
  151. {
  152. CXTPCommandBar* pToolBar = (CXTPToolBar*)pCmd->m_pOther;
  153. if (pToolBar)
  154. {
  155. CXTPControlSizeComboBox* pSizeCombo = (CXTPControlSizeComboBox*)pToolBar->GetControls()->GetAt(pCmd->m_nIndex);
  156. if (pSizeCombo->GetType() == xtpControlComboBox)
  157. {
  158. CHARFORMAT& cf = GetCharFormatSelection();
  159. pSizeCombo->SetTwipSize( (cf.dwMask & CFM_SIZE) ? cf.yHeight : -1);
  160. }
  161. }
  162. pCmd->Enable(TRUE);
  163. }
  164. void CSmartLayoutView::OnUpdateSelectorText(CCmdUI* pCmd)
  165. {
  166. CXTPCommandBar* pToolBar = (CXTPToolBar*)pCmd->m_pOther;
  167. if (pToolBar)
  168. {
  169. CHARFORMAT& cfm = GetCharFormatSelection( );
  170. CXTPControlColorSelector* pSelector = (CXTPControlColorSelector*)pToolBar->GetControls()->GetAt(pCmd->m_nIndex);
  171. pSelector->SetColor(cfm.dwEffects & CFE_AUTOCOLOR? -1: cfm.crTextColor);
  172. }
  173. pCmd->Enable(TRUE);
  174. }
  175. void CSmartLayoutView::OnButtonText()
  176. {
  177. CHARFORMAT& cfm = GetCharFormatSelection( );
  178. cfm.dwMask |= CFM_COLOR;
  179. cfm.dwEffects &= ~CFE_AUTOCOLOR ;
  180. cfm.crTextColor = m_clr;
  181. GetRichEditCtrl().SetSelectionCharFormat(cfm);
  182. }
  183. void CSmartLayoutView::OnSelectorText(NMHDR* pNMHDR, LRESULT* pResult)
  184. {
  185. NMXTPCONTROL* tagNMCONTROL = (NMXTPCONTROL*)pNMHDR;
  186. CXTPControlColorSelector* pControl = (CXTPControlColorSelector*)tagNMCONTROL->pControl;
  187. m_clr = pControl->GetColor();
  188. OnButtonText();
  189. *pResult = 1;
  190. }
  191. void CSmartLayoutView::OnUpdateText(CCmdUI* pCmd)
  192. {
  193. CXTPCommandBar* pToolBar = (CXTPToolBar*)pCmd->m_pOther;
  194. if (pToolBar)
  195. {
  196. CXTPControlPopupColor* pPopup = (CXTPControlPopupColor*)pToolBar->GetControls()->GetAt(pCmd->m_nIndex);
  197. pPopup->SetColor(m_clr);
  198. }
  199. pCmd->Enable(TRUE);
  200. }
  201. void CSmartLayoutView::OnTextAuto()
  202. {
  203. CHARFORMAT& cfm = GetCharFormatSelection( );
  204. cfm.dwMask |= CFM_COLOR;
  205. cfm.dwEffects |= CFE_AUTOCOLOR ;
  206. GetRichEditCtrl().SetSelectionCharFormat(cfm);
  207. m_clr = GetSysColor(COLOR_BTNTEXT);
  208. }
  209. void CSmartLayoutView::OnUpdateTextAuto(CCmdUI* pCmd)
  210. {
  211. CHARFORMAT& cfm = GetCharFormatSelection( );
  212. pCmd->SetCheck(cfm.dwEffects & CFE_AUTOCOLOR? TRUE: FALSE);
  213. }
  214. void CSmartLayoutView::OnTextMore()
  215. {
  216. CColorDialog cd(m_clr);
  217. if (cd.DoModal())
  218. {
  219. m_clr = cd.GetColor();
  220. OnButtonText();
  221. }
  222. }
  223. void CSmartLayoutView::OnEditUndo(NMHDR* pNMHDR, LRESULT* pResult)
  224. {
  225. if (((NMXTPCONTROL*)pNMHDR)->pControl->GetType() == xtpControlSplitButtonPopup)
  226. {
  227. CRichEditView::OnEditUndo();
  228. *pResult = 1; // Handled;
  229. }
  230. CXTPControlListBox* pControlListBox = DYNAMIC_DOWNCAST(CXTPControlListBox, ((NMXTPCONTROL*)pNMHDR)->pControl);
  231. if (pControlListBox)
  232. {
  233. CString str;
  234. str.Format(_T("Undo last %i actions"), pControlListBox->GetListCtrl()->GetSelCount());
  235. AfxMessageBox(str);
  236. *pResult = 1; // Handled;
  237. }
  238. }
  239. CXTPControlStatic* CSmartLayoutView::FindInfoControl(CXTPControl* pControl)
  240. {
  241. CXTPCommandBar* pCommandBar = pControl->GetParent();
  242. for (int i = 0; i < pCommandBar->GetControls()->GetCount(); i++)
  243. {
  244. CXTPControlStatic* pControlStatic = DYNAMIC_DOWNCAST(CXTPControlStatic, pCommandBar->GetControl(i));
  245. if (pControlStatic && pControlStatic->GetID() == pControl->GetID())
  246. {
  247. return pControlStatic;
  248. }
  249. }
  250. return NULL;
  251. }
  252. void CSmartLayoutView::OnListBoxControlSelChange(NMHDR* pNMHDR, LRESULT* pRes)
  253. {
  254. ASSERT(pNMHDR != NULL);
  255. ASSERT(pRes != NULL);
  256. CXTPControlListBox* pControlListBox = DYNAMIC_DOWNCAST(CXTPControlListBox, ((NMXTPCONTROL*)pNMHDR)->pControl);
  257. if (pControlListBox)
  258. {
  259. CXTPControlStatic* pInfo = FindInfoControl(pControlListBox);
  260. if (pInfo)
  261. {
  262. CString str;
  263. str.Format(_T("Undo %i Actions"), pControlListBox->GetListCtrl()->GetSelCount());
  264. pInfo->SetCaption(str);
  265. pInfo->DelayRedrawParent();
  266. }
  267. *pRes = 1;
  268. }
  269. }
  270. void CSmartLayoutView::OnListBoxControlPoup(NMHDR* pNMHDR, LRESULT* pRes)
  271. {
  272. ASSERT(pNMHDR != NULL);
  273. ASSERT(pRes != NULL);
  274. CXTPControlListBox* pControlListBox = DYNAMIC_DOWNCAST(CXTPControlListBox, ((NMXTPCONTROL*)pNMHDR)->pControl);
  275. if (pControlListBox)
  276. {
  277. CListBox* pListBox = pControlListBox->GetListCtrl();
  278. pListBox->ResetContent();
  279. int nCount = RAND_S() % 20 + 3;
  280. for (int i = 0; i < nCount; i++)
  281. {
  282. CString str;
  283. str.Format(_T("Undo String %i"), i + 1);
  284. pListBox->AddString(str);
  285. }
  286. CXTPControlStatic* pInfo = FindInfoControl(pControlListBox);
  287. if (pInfo)
  288. {
  289. CString str;
  290. pInfo->SetCaption(_T("Undo 0 Actions"));
  291. pInfo->DelayRedrawParent();
  292. }
  293. *pRes = 1;
  294. }
  295. }
  296. void CSmartLayoutView::OnColumnsSelChange(NMHDR* pNMHDR, LRESULT* pRes)
  297. {
  298. ASSERT(pNMHDR != NULL);
  299. ASSERT(pRes != NULL);
  300. CControlColumns* pControlColumns = DYNAMIC_DOWNCAST(CControlColumns, ((NMXTPCONTROL*)pNMHDR)->pControl);
  301. if (pControlColumns)
  302. {
  303. CXTPControlStatic* pInfo = FindInfoControl(pControlColumns);
  304. if (pInfo)
  305. {
  306. int nCount = pControlColumns->m_szItemsActive.cx;
  307. CString str = _T("Cancel");
  308. if (nCount != 0)
  309. {
  310. str.Format(_T("%i Columns"), nCount);
  311. }
  312. pInfo->SetWidth(pControlColumns->m_szItem.cx * pControlColumns->m_szItemsVisible.cx);
  313. pInfo->SetCaption(str);
  314. pInfo->DelayRedrawParent();
  315. }
  316. *pRes = 1;
  317. }
  318. }
  319. void CSmartLayoutView::OnInsertTable(NMHDR* pNMHDR, LRESULT* pResult)
  320. {
  321. CControlTable* pControlTable = DYNAMIC_DOWNCAST(CControlTable, ((NMXTPCONTROL*)pNMHDR)->pControl);
  322. if (pControlTable)
  323. {
  324. CSize szActive = pControlTable->m_szResult;
  325. CString str;
  326. str.Format(_T("<%i x %i Table>"), szActive.cx, szActive.cy);
  327. GetRichEditCtrl().ReplaceSel(str);
  328. *pResult = 1; // Handled;
  329. }
  330. }
  331. void CSmartLayoutView::OnInsertColumns(NMHDR* pNMHDR, LRESULT* pResult)
  332. {
  333. CControlColumns* pControlColumns = DYNAMIC_DOWNCAST(CControlColumns, ((NMXTPCONTROL*)pNMHDR)->pControl);
  334. if (pControlColumns)
  335. {
  336. int nCount = pControlColumns->m_szResult.cx;
  337. CString str;
  338. str.Format(_T("<%i Columns>"), nCount);
  339. GetRichEditCtrl().ReplaceSel(str);
  340. *pResult = 1; // Handled;
  341. }
  342. }
  343. void CSmartLayoutView::OnTableSelChange(NMHDR* pNMHDR, LRESULT* pRes)
  344. {
  345. ASSERT(pNMHDR != NULL);
  346. ASSERT(pRes != NULL);
  347. CControlTable* pControlTable = DYNAMIC_DOWNCAST(CControlTable, ((NMXTPCONTROL*)pNMHDR)->pControl);
  348. if (pControlTable)
  349. {
  350. CXTPControlStatic* pInfo = FindInfoControl(pControlTable);
  351. if (pInfo)
  352. {
  353. CSize szActive = pControlTable->m_szItemsActive;
  354. CString str = _T("Cancel");
  355. if (szActive != CSize(0))
  356. {
  357. str.Format(_T("%i x %i Table"), szActive.cx, szActive.cy);
  358. }
  359. pInfo->SetWidth(pControlTable->m_szItem.cx * pControlTable->m_szItemsVisible.cx);
  360. pInfo->SetCaption(str);
  361. pInfo->DelayRedrawParent();
  362. }
  363. *pRes = 1;
  364. }
  365. }
  366. enum Borders {borderTop = 1, borderLeft = 2, borderRight = 4, borderBottom = 8, borderInsideVert = 16, borderInsideHorz = 32,
  367. borderOutside = borderBottom | borderTop | borderLeft | borderRight,
  368. borderInside = borderInsideVert | borderInsideHorz,
  369. borderAll = borderInside | borderOutside};
  370. void CSmartLayoutView::SwitchBorders(DWORD dwState)
  371. {
  372. if (m_dwBorders & dwState) m_dwBorders &= ~dwState; else m_dwBorders |= dwState;
  373. }
  374. void CSmartLayoutView::OnBorders(UINT nID)
  375. {
  376. switch (nID)
  377. {
  378. case ID_BDR_TOP: SwitchBorders(borderTop); break;
  379. case ID_BDR_LEFT: SwitchBorders(borderLeft); break;
  380. case ID_BDR_RIGHT: SwitchBorders(borderRight); break;
  381. case ID_BDR_BOTTOM: SwitchBorders(borderBottom); break;
  382. case ID_BDR_NONE: m_dwBorders = 0; break;
  383. case ID_BDR_OUTSIDE: SwitchBorders(borderOutside); break;
  384. case ID_BDR_ALL: SwitchBorders(borderAll); break;
  385. case ID_BDR_INSIDE_VERT: SwitchBorders(borderInsideVert); break;
  386. case ID_BDR_INSIDE_HORZ: SwitchBorders(borderInsideHorz); break;
  387. case ID_BDR_INSIDE: SwitchBorders(borderInside); break;
  388. }
  389. }
  390. void CSmartLayoutView::OnUpdateBorders(CCmdUI* pCmdUI)
  391. {
  392. switch (pCmdUI->m_nID)
  393. {
  394. case ID_BDR_TOP: pCmdUI->SetCheck(m_dwBorders & borderTop? TRUE: FALSE); break;
  395. case ID_BDR_LEFT: pCmdUI->SetCheck(m_dwBorders & borderLeft? TRUE: FALSE); break;
  396. case ID_BDR_RIGHT: pCmdUI->SetCheck(m_dwBorders & borderRight? TRUE: FALSE); break;
  397. case ID_BDR_BOTTOM: pCmdUI->SetCheck(m_dwBorders & borderBottom? TRUE: FALSE); break;
  398. case ID_BDR_OUTSIDE : pCmdUI->SetCheck((m_dwBorders & borderOutside) == borderOutside? TRUE: FALSE); break;
  399. case ID_BDR_ALL : pCmdUI->SetCheck((m_dwBorders & borderAll) == borderAll? TRUE: FALSE); break;
  400. case ID_BDR_INSIDE_VERT: pCmdUI->SetCheck(m_dwBorders & borderInsideVert? TRUE: FALSE); break;
  401. case ID_BDR_INSIDE_HORZ: pCmdUI->SetCheck(m_dwBorders & borderInsideHorz? TRUE: FALSE); break;
  402. case ID_BDR_INSIDE: pCmdUI->SetCheck((m_dwBorders & borderInside) == borderInside? TRUE: FALSE); break;
  403. }
  404. }
  405. void CSmartLayoutView::OnEnableCommand(CCmdUI* pCmdUI)
  406. {
  407. pCmdUI->Enable();
  408. }