home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / APPUI.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.9 KB  |  277 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_CORE2_SEG
  14. #pragma code_seg(AFX_CORE2_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CWinApp User Interface Extensions
  24.  
  25. void CWinApp::OnAppExit()
  26. {
  27.     // same as double-clicking on main window close box
  28.     ASSERT(m_pMainWnd != NULL);
  29.     m_pMainWnd->SendMessage(WM_CLOSE);
  30. }
  31.  
  32. #ifdef AFX_CORE3_SEG
  33. #pragma code_seg(AFX_CORE3_SEG)
  34. #endif
  35.  
  36. void CWinApp::HideApplication()
  37. {
  38.     ASSERT_VALID(m_pMainWnd);
  39.  
  40.     // hide the application's windows before closing all the documents
  41.     m_pMainWnd->ShowWindow(SW_HIDE);
  42.     m_pMainWnd->ShowOwnedPopups(FALSE);
  43.  
  44.     // put the window at the bottom of zorder, so it isn't activated
  45.     m_pMainWnd->SetWindowPos(&CWnd::wndBottom, 0, 0, 0, 0,
  46.         SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
  47. }
  48.  
  49. void CWinApp::DoWaitCursor(int nCode)
  50. {
  51.     // 0 => restore, 1=> begin, -1=> end
  52.     ASSERT(nCode == 0 || nCode == 1 || nCode == -1);
  53.     ASSERT(afxData.hcurWait != NULL);
  54.     AfxLockGlobals(CRIT_WAITCURSOR);
  55.     m_nWaitCursorCount += nCode;
  56.     if (m_nWaitCursorCount > 0)
  57.     {
  58.         HCURSOR hcurPrev = ::SetCursor(afxData.hcurWait);
  59.         if (nCode > 0 && m_nWaitCursorCount == 1)
  60.             m_hcurWaitCursorRestore = hcurPrev;
  61.     }
  62.     else
  63.     {
  64.         // turn everything off
  65.         m_nWaitCursorCount = 0;     // prevent underflow
  66.         ::SetCursor(m_hcurWaitCursorRestore);
  67.     }
  68.     AfxUnlockGlobals(CRIT_WAITCURSOR);
  69. }
  70.  
  71. void CWinApp::EnableModeless(BOOL bEnable)
  72. {
  73. #ifdef _AFX_NO_OLE_SUPPORT
  74.     UNUSED(bEnable);
  75. #endif
  76.  
  77.     // no-op if main window is NULL or not a CFrameWnd
  78.     CWnd* pMainWnd = AfxGetMainWnd();
  79.     if (pMainWnd == NULL || !pMainWnd->IsFrameWnd())
  80.         return;
  81.  
  82. #ifndef _AFX_NO_OLE_SUPPORT
  83.     // check if notify hook installed
  84.     ASSERT_KINDOF(CFrameWnd, pMainWnd);
  85.     CFrameWnd* pFrameWnd = (CFrameWnd*)pMainWnd;
  86.     if (pFrameWnd->m_pNotifyHook != NULL)
  87.         pFrameWnd->m_pNotifyHook->OnEnableModeless(bEnable);
  88. #endif
  89. }
  90.  
  91. int CWinApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
  92. {
  93.     ASSERT_VALID(this);
  94.  
  95.     // disable windows for modal dialog
  96.     EnableModeless(FALSE);
  97.     HWND hWndTop;
  98.     CWnd* pWnd = CWnd::GetSafeOwner(NULL, &hWndTop);
  99.  
  100.     // set help context if possible
  101.     DWORD* pdwContext = &m_dwPromptContext;
  102.     if (pWnd != NULL)
  103.     {
  104.         // use app-level context or frame level context
  105.         ASSERT_VALID(pWnd);
  106.         CWnd* pMainWnd = pWnd->GetTopLevelParent();
  107.         ASSERT_VALID(pMainWnd);
  108.         if (pMainWnd->IsFrameWnd())
  109.             pdwContext = &((CFrameWnd*)pMainWnd)->m_dwPromptContext;
  110.     }
  111.  
  112.     ASSERT(pdwContext != NULL);
  113.     DWORD dwOldPromptContext = *pdwContext;
  114.     if (nIDPrompt != 0)
  115.         *pdwContext = HID_BASE_PROMPT+nIDPrompt;
  116.  
  117.     // determine icon based on type specified
  118.     if ((nType & MB_ICONMASK) == 0)
  119.     {
  120.         switch (nType & MB_TYPEMASK)
  121.         {
  122.         case MB_OK:
  123.         case MB_OKCANCEL:
  124.             nType |= MB_ICONEXCLAMATION;
  125.             break;
  126.  
  127.         case MB_YESNO:
  128.         case MB_YESNOCANCEL:
  129.             nType |= MB_ICONEXCLAMATION;
  130.             break;
  131.  
  132.         case MB_ABORTRETRYIGNORE:
  133.         case MB_RETRYCANCEL:
  134.             // No default icon for these types, since they are rarely used.
  135.             // The caller should specify the icon.
  136.             break;
  137.  
  138. #ifdef _MAC
  139.         case MB_SAVEDONTSAVECANCEL:
  140.             nType |= MB_ICONEXCLAMATION;
  141.             break;
  142. #endif
  143.         }
  144.     }
  145.  
  146. #ifdef _DEBUG
  147.     if ((nType & MB_ICONMASK) == 0)
  148.         TRACE0("Warning: no icon specified for message box.\n");
  149. #endif
  150.  
  151.     _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
  152.     int nResult =
  153.         ::MessageBox(pWnd->GetSafeHwnd(), lpszPrompt, m_pszAppName, nType);
  154.     *pdwContext = dwOldPromptContext;
  155.  
  156.     // re-enable windows
  157.     if (hWndTop != NULL)
  158.         ::EnableWindow(hWndTop, TRUE);
  159.     EnableModeless(TRUE);
  160.  
  161.     return nResult;
  162. }
  163.  
  164. int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp)
  165. {
  166.     CWinApp* pApp = AfxGetApp();
  167.     return pApp->DoMessageBox(lpszText, nType, nIDHelp);
  168. }
  169.  
  170. int AFXAPI AfxMessageBox(UINT nIDPrompt, UINT nType, UINT nIDHelp)
  171. {
  172.     CString string;
  173.     if (!string.LoadString(nIDPrompt))
  174.     {
  175.         TRACE1("Error: failed to load message box prompt string 0x%04x.\n",
  176.             nIDPrompt);
  177.         ASSERT(FALSE);
  178.     }
  179.     if (nIDHelp == (UINT)-1)
  180.         nIDHelp = nIDPrompt;
  181.     return AfxGetApp()->DoMessageBox(string, nType, nIDHelp);
  182. }
  183.  
  184. int CWnd::MessageBox(LPCTSTR lpszText, LPCTSTR lpszCaption, UINT nType)
  185. {
  186.     if (lpszCaption == NULL)
  187.         lpszCaption = AfxGetAppName();
  188.     _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
  189.     int nResult = ::MessageBox(GetSafeHwnd(), lpszText, lpszCaption, nType);
  190.     return nResult;
  191. }
  192.  
  193. BOOL CWinApp::SaveAllModified()
  194. {
  195.     if (m_pDocManager != NULL)
  196.         return m_pDocManager->SaveAllModified();
  197.     return TRUE;
  198. }
  199.  
  200. void CWinApp::AddToRecentFileList(LPCTSTR lpszPathName)
  201. {
  202.     ASSERT_VALID(this);
  203.     ASSERT(lpszPathName != NULL);
  204.     ASSERT(AfxIsValidString(lpszPathName));
  205.  
  206.     if (m_pRecentFileList != NULL)
  207.     {
  208.         // fully qualify the path name
  209.         TCHAR szTemp[_MAX_PATH];
  210.         AfxFullPath(szTemp, lpszPathName);
  211.  
  212.         // then add to recent file list
  213.         m_pRecentFileList->Add(szTemp);
  214.     }
  215. }
  216.  
  217. CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)
  218. {
  219.     ASSERT(m_pDocManager != NULL);
  220.     return m_pDocManager->OpenDocumentFile(lpszFileName);
  221. }
  222.  
  223. void CWinApp::CloseAllDocuments(BOOL bEndSession)
  224. {
  225.     if (m_pDocManager != NULL)
  226.         m_pDocManager->CloseAllDocuments(bEndSession);
  227. }
  228.  
  229. /////////////////////////////////////////////////////////////////////////////
  230. // MRU file list default implementation
  231.  
  232. void CWinApp::OnUpdateRecentFileMenu(CCmdUI* pCmdUI)
  233. {
  234.     ASSERT_VALID(this);
  235.     if (m_pRecentFileList == NULL) // no MRU files
  236.         pCmdUI->Enable(FALSE);
  237.     else
  238.         m_pRecentFileList->UpdateMenu(pCmdUI);
  239. }
  240.  
  241. /////////////////////////////////////////////////////////////////////////////
  242. // DDE and ShellExecute support
  243.  
  244. #ifndef _MAC
  245. BOOL CWinApp::OnDDECommand(LPTSTR lpszCommand)
  246. {
  247.     if (m_pDocManager != NULL)
  248.         return m_pDocManager->OnDDECommand(lpszCommand);
  249.     else
  250.         return FALSE;
  251. }
  252. #endif
  253.  
  254. /////////////////////////////////////////////////////////////////////////////
  255. // MRU file list default implementation
  256.  
  257. BOOL CWinApp::OnOpenRecentFile(UINT nID)
  258. {
  259.     ASSERT_VALID(this);
  260.     ASSERT(m_pRecentFileList != NULL);
  261.  
  262.     ASSERT(nID >= ID_FILE_MRU_FILE1);
  263.     ASSERT(nID < ID_FILE_MRU_FILE1 + (UINT)m_pRecentFileList->GetSize());
  264.     int nIndex = nID - ID_FILE_MRU_FILE1;
  265.     ASSERT((*m_pRecentFileList)[nIndex].GetLength() != 0);
  266.  
  267.     TRACE2("MRU: open file (%d) '%s'.\n", (nIndex) + 1,
  268.             (LPCTSTR)(*m_pRecentFileList)[nIndex]);
  269.  
  270.     if (OpenDocumentFile((*m_pRecentFileList)[nIndex]) == NULL)
  271.         m_pRecentFileList->Remove(nIndex);
  272.  
  273.     return TRUE;
  274. }
  275.  
  276. /////////////////////////////////////////////////////////////////////////////
  277.