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

对话框与窗口

开发平台:

Visual C++

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3. #include "stdafx.h"
  4. #include "MDITextEditor.h"
  5. #include "MainFrm.h"
  6. #include "MDITextEditorDoc.h"
  7. #include "MDITextEditorView.h"
  8. #include <locale.h>
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CMainFrame
  16. IMPLEMENT_DYNAMIC(CMainFrame, CMDIFrameWnd)
  17. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  18. //{{AFX_MSG_MAP(CMainFrame)
  19. ON_WM_CREATE()
  20. ON_MESSAGE(WM_COPYDATA, OnAppInstance)
  21. ON_UPDATE_COMMAND_UI(ID_INDICATOR_OVR, OnUpdateKeyIndicator)
  22. ON_UPDATE_COMMAND_UI(ID_INDICATOR_ROWCOL, OnUpdateRowColIndicator)
  23. ON_WM_ACTIVATE()
  24. //}}AFX_MSG_MAP
  25. ON_COMMAND(XTP_ID_CUSTOMIZE, OnCustomize)
  26. ON_WM_CLOSE()
  27. END_MESSAGE_MAP()
  28. static UINT indicators[] =
  29. {
  30. ID_SEPARATOR,           // status line indicator
  31. ID_INDICATOR_ROWCOL,
  32. ID_INDICATOR_CAPS,
  33. ID_INDICATOR_NUM,
  34. ID_INDICATOR_OVR,
  35. ID_INDICATOR_SCRL,
  36. };
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CMainFrame construction/destruction
  39. CMainFrame::CMainFrame()
  40. {
  41. // set the current user locale
  42. // to allow some text functions (like isleadbyte) works correctly
  43. //
  44. _tsetlocale(LC_ALL, _T(""));
  45. }
  46. CMainFrame::~CMainFrame()
  47. {
  48. }
  49. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  50. {
  51. if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  52. return -1;
  53. if (!m_wndStatusBar.Create(this) ||
  54. !m_wndStatusBar.SetIndicators(indicators,
  55.   sizeof(indicators)/sizeof(UINT)))
  56. {
  57. TRACE0("Failed to create status barn");
  58. return -1;      // fail to create
  59. }
  60. if (!InitCommandBars())
  61. return -1;
  62. XTPPaintManager()->SetTheme(xtpThemeWhidbey);
  63. CXTPCommandBars* pCommandBars = GetCommandBars();
  64. CXTPCommandBar* pMenuBar = pCommandBars->SetMenu(_T("Menu Bar"), IDR_MAINFRAME);
  65. pMenuBar->SetFlags(xtpFlagAddMDISysPopup);
  66. CXTPToolBar* pCommandBar = (CXTPToolBar*)pCommandBars->Add(_T("Standard"), xtpBarTop);
  67. if (!pCommandBar ||
  68. !pCommandBar->LoadToolBar(IDR_MAINFRAME))
  69. {
  70. TRACE0("Failed to create toolbarn");
  71. return -1;
  72. }
  73. // Load the previous state for toolbars and menus.
  74. LoadCommandBars(_T("CommandBars"));
  75. VERIFY(m_MTIClientWnd.Attach(this));
  76. m_MTIClientWnd.EnableToolTips();
  77. m_MTIClientWnd.GetPaintManager()->m_bShowIcons = FALSE;
  78. m_MTIClientWnd.SetNewTabPosition(xtpWorkspaceNewTabLeftMost);
  79. m_MTIClientWnd.SetFlags(xtpWorkspaceHideArrowsAlways | xtpWorkspaceHideClose | xtpWorkspaceShowActiveFiles);
  80. // Initialize row, col indicator.
  81. m_wndStatusBar.SetPaneInfo(m_wndStatusBar.CommandToIndex(
  82. ID_INDICATOR_ROWCOL), ID_INDICATOR_ROWCOL, SBPS_NORMAL, 75);
  83. SetRowCol(0,0);
  84. return 0;
  85. }
  86. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  87. {
  88. if( !CMDIFrameWnd::PreCreateWindow(cs) )
  89. return FALSE;
  90.     // Set the class name to be the app's single instance class name 
  91. cs.lpszClass = CLASSNAME_APPINSTANCE;
  92. return TRUE;
  93. }
  94. /////////////////////////////////////////////////////////////////////////////
  95. // CMainFrame diagnostics
  96. #ifdef _DEBUG
  97. void CMainFrame::AssertValid() const
  98. {
  99. CMDIFrameWnd::AssertValid();
  100. }
  101. void CMainFrame::Dump(CDumpContext& dc) const
  102. {
  103. CMDIFrameWnd::Dump(dc);
  104. }
  105. #endif //_DEBUG
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CMainFrame message handlers
  108. LRESULT CMainFrame::OnAppInstance(WPARAM wParam, LPARAM lParam)
  109. {
  110. COPYDATASTRUCT* pCopyData = (COPYDATASTRUCT*)lParam;
  111. if (pCopyData->dwData == cdwData_FileName)
  112. {
  113. theApp.OpenDocumentFile((LPCTSTR)pCopyData->lpData);
  114. return TRUE;
  115. }
  116. return FALSE;
  117. }
  118. void CMainFrame::OnUpdateKeyIndicator(CCmdUI* pCmdUI)
  119. {
  120. pCmdUI->Enable(theApp.m_bInsertKey);
  121. }
  122. void CMainFrame::OnUpdateRowColIndicator(CCmdUI* pCmdUI)
  123. {
  124. pCmdUI->Enable(TRUE);
  125. }
  126. BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
  127. {
  128. if (pMsg->message == WM_KEYDOWN)
  129. {
  130. // Ctrl + Tab key pressed.
  131. if ((::GetKeyState(VK_CONTROL) & 0x8000) && (pMsg->wParam == VK_TAB))
  132. {
  133. if (theApp.m_pDocManager->GetOpenDocumentCount() < 2)
  134. {
  135. MessageBeep((UINT)-1);
  136. }
  137. }
  138. }
  139. return CMDIFrameWnd::PreTranslateMessage(pMsg);
  140. }
  141. void CMainFrame::SetRowCol(int nRow, int nCol)
  142. {
  143. CString csRowCol;
  144. csRowCol.Format(_T("Ln %d, Col %d"), nRow, nCol);
  145. m_wndStatusBar.SetPaneText(m_wndStatusBar.CommandToIndex(
  146. ID_INDICATOR_ROWCOL), csRowCol);
  147. }
  148. void CMainFrame::UpdateMenuPopups(int nIndex)
  149. {
  150. CMenu *pMainMenu = GetMenu();
  151. ASSERT_VALID(pMainMenu);
  152. CMenu *pSubMenu = pMainMenu->GetSubMenu(nIndex);
  153. OnInitMenuPopup(pSubMenu, 0, FALSE);
  154. CMenu *pAdvSubMenu = pSubMenu->GetSubMenu(16);
  155. OnInitMenuPopup(pAdvSubMenu, 0, FALSE);
  156. }
  157. void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
  158. {
  159. CMDIFrameWnd::OnActivate(nState, pWndOther, bMinimized);
  160. if (nState != WA_INACTIVE)
  161. {
  162. POSITION pos = theApp.m_pDocTemplate->GetFirstDocPosition();
  163. while(pos)
  164. {
  165. CMDITextEditorDoc* pDoc = DYNAMIC_DOWNCAST(CMDITextEditorDoc,
  166. theApp.m_pDocTemplate->GetNextDoc(pos));
  167. if (pDoc)
  168. pDoc->CheckFileModified(pDoc->GetPathName());
  169. }
  170. }
  171. }
  172. void CMainFrame::OnCustomize()
  173. {
  174. // Get a pointer to the command bars object.
  175. CXTPCommandBars* pCommandBars = GetCommandBars();
  176. if(pCommandBars != NULL)
  177. {
  178. // Instanciate the customize dialog object.
  179. CXTPCustomizeSheet dlg(pCommandBars);
  180. // Add the options page to the customize dialog.
  181. CXTPCustomizeOptionsPage pageOptions(&dlg);
  182. dlg.AddPage(&pageOptions);
  183. // Add the commands page to the customize dialog.
  184. CXTPCustomizeCommandsPage* pCommands = dlg.GetCommandsPage();
  185. pCommands->AddCategories(IDR_MDITEXTYPE);
  186. // Use the command bar manager to initialize the
  187. // customize dialog.
  188. pCommands->InsertAllCommandsCategory();
  189. pCommands->InsertBuiltInMenus(IDR_MDITEXTYPE);
  190. pCommands->InsertNewMenuCategory();
  191. // Dispaly the dialog.
  192. dlg.DoModal();
  193. }
  194. }
  195. void CMainFrame::OnClose() 
  196. {
  197. // Save the current state for toolbars and menus.
  198. SaveCommandBars(_T("CommandBars"));
  199. CMDIFrameWnd::OnClose();
  200. }
  201. void CMainFrame::OnSetPreviewMode(BOOL bPreview, CPrintPreviewState* pState) 
  202. {
  203. // Toggle CommandBars
  204. GetCommandBars()->OnSetPreviewMode(bPreview);
  205. // Toggle Tab Client
  206. m_MTIClientWnd.ShowWorkspace(!bPreview);
  207. CMDIFrameWnd::OnSetPreviewMode(bPreview, pState);
  208. }