home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / appui1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  5.4 KB  |  220 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 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_CORE3_SEG
  14. #pragma code_seg(AFX_CORE3_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CWinApp UI related functions
  24.  
  25. void CWinApp::EnableModeless(BOOL bEnable)
  26. {
  27. #ifdef _AFX_NO_OLE_SUPPORT
  28.     UNUSED(bEnable);
  29. #endif
  30.  
  31.     // no-op if main window is NULL or not a CFrameWnd
  32.     CWnd* pMainWnd = AfxGetMainWnd();
  33.     if (pMainWnd == NULL || !pMainWnd->IsFrameWnd())
  34.         return;
  35.  
  36. #ifndef _AFX_NO_OLE_SUPPORT
  37.     // check if notify hook installed
  38.     ASSERT_KINDOF(CFrameWnd, pMainWnd);
  39.     CFrameWnd* pFrameWnd = (CFrameWnd*)pMainWnd;
  40.     if (pFrameWnd->m_pNotifyHook != NULL)
  41.         pFrameWnd->m_pNotifyHook->OnEnableModeless(bEnable);
  42. #endif
  43. }
  44.  
  45. int CWinApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
  46. {
  47.     // disable windows for modal dialog
  48.     EnableModeless(FALSE);
  49.     HWND hWndTop;
  50.     HWND hWnd = CWnd::GetSafeOwner_(NULL, &hWndTop);
  51.  
  52.     // set help context if possible
  53.     DWORD* pdwContext = NULL;
  54.     if (hWnd != NULL)
  55.     {
  56.         // use app-level context or frame level context
  57.         LRESULT lResult = ::SendMessage(hWndTop, WM_HELPPROMPTADDR, 0, 0);
  58.         if (lResult != 0)
  59.             pdwContext = (DWORD*)lResult;
  60.     }
  61.     // for backward compatibility use app context if possible
  62.     if (pdwContext == NULL && this != NULL)
  63.         pdwContext = &m_dwPromptContext;
  64.  
  65.     DWORD dwOldPromptContext = 0;
  66.     if (pdwContext != NULL)
  67.     {
  68.         // save old prompt context for restoration later
  69.         dwOldPromptContext = *pdwContext;
  70.         if (nIDPrompt != 0)
  71.             *pdwContext = HID_BASE_PROMPT+nIDPrompt;
  72.     }
  73.  
  74.     // determine icon based on type specified
  75.     if ((nType & MB_ICONMASK) == 0)
  76.     {
  77.         switch (nType & MB_TYPEMASK)
  78.         {
  79.         case MB_OK:
  80.         case MB_OKCANCEL:
  81.             nType |= MB_ICONEXCLAMATION;
  82.             break;
  83.  
  84.         case MB_YESNO:
  85.         case MB_YESNOCANCEL:
  86.             nType |= MB_ICONEXCLAMATION;
  87.             break;
  88.  
  89.         case MB_ABORTRETRYIGNORE:
  90.         case MB_RETRYCANCEL:
  91.             // No default icon for these types, since they are rarely used.
  92.             // The caller should specify the icon.
  93.             break;
  94.         }
  95.     }
  96.  
  97. #ifdef _DEBUG
  98.     if ((nType & MB_ICONMASK) == 0)
  99.         TRACE0("Warning: no icon specified for message box.\n");
  100. #endif
  101.  
  102.     TCHAR szAppName[_MAX_PATH];
  103.     LPCTSTR pszAppName;
  104.     if (this != NULL)
  105.         pszAppName = m_pszAppName;
  106.     else
  107.     {
  108.         pszAppName = szAppName;
  109.         GetModuleFileName(NULL, szAppName, _MAX_PATH);
  110.     }
  111.  
  112.     int nResult =
  113.         ::MessageBox(hWnd, lpszPrompt, pszAppName, nType);
  114.  
  115.     // restore prompt context if possible
  116.     if (pdwContext != NULL)
  117.         *pdwContext = dwOldPromptContext;
  118.  
  119.     // re-enable windows
  120.     if (hWndTop != NULL)
  121.         ::EnableWindow(hWndTop, TRUE);
  122.     EnableModeless(TRUE);
  123.  
  124.     return nResult;
  125. }
  126.  
  127. int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp)
  128. {
  129.     CWinApp* pApp = AfxGetApp();
  130.     if (pApp != NULL)
  131.         return pApp->DoMessageBox(lpszText, nType, nIDHelp);
  132.     else
  133.         return pApp->CWinApp::DoMessageBox(lpszText, nType, nIDHelp);
  134. }
  135.  
  136. int AFXAPI AfxMessageBox(UINT nIDPrompt, UINT nType, UINT nIDHelp)
  137. {
  138.     CString string;
  139.     if (!string.LoadString(nIDPrompt))
  140.     {
  141.         TRACE1("Error: failed to load message box prompt string 0x%04x.\n",
  142.             nIDPrompt);
  143.         ASSERT(FALSE);
  144.     }
  145.     if (nIDHelp == (UINT)-1)
  146.         nIDHelp = nIDPrompt;
  147.     return AfxMessageBox(string, nType, nIDHelp);
  148. }
  149.  
  150. ////////////////////////////////////////////////////////////////////////////
  151. // UI related CWnd functions
  152.  
  153. HWND PASCAL CWnd::GetSafeOwner_(HWND hParent, HWND* pWndTop)
  154. {
  155.     // get window to start with
  156.     HWND hWnd = hParent;
  157.     if (hWnd == NULL)
  158.     {
  159.         CFrameWnd* pFrame = CCmdTarget::GetRoutingFrame_();
  160.         if (pFrame != NULL)
  161.             hWnd = pFrame->GetSafeHwnd();
  162.         else
  163.             hWnd = AfxGetMainWnd()->GetSafeHwnd();
  164.     }
  165.  
  166.     // a popup window cannot be owned by a child window
  167.     while (hWnd != NULL && (::GetWindowLong(hWnd, GWL_STYLE) & WS_CHILD))
  168.         hWnd = ::GetParent(hWnd);
  169.  
  170.     // determine toplevel window to disable as well
  171.     HWND hWndTop = hWnd, hWndTemp = hWnd;
  172.     for (;;)
  173.     {
  174.         if (hWndTemp == NULL)
  175.             break;
  176.         else
  177.             hWndTop = hWndTemp;
  178.         hWndTemp = ::GetParent(hWndTop);
  179.     }
  180.  
  181.     // get last active popup of first non-child that was found
  182.     if (hParent == NULL && hWnd != NULL)
  183.         hWnd = ::GetLastActivePopup(hWnd);
  184.  
  185.     // disable and store top level parent window if specified
  186.     if (pWndTop != NULL)
  187.     {
  188.         if (hWndTop != NULL && ::IsWindowEnabled(hWndTop) && hWndTop != hWnd)
  189.         {
  190.             *pWndTop = hWndTop;
  191.             ::EnableWindow(hWndTop, FALSE);
  192.         }
  193.         else
  194.             *pWndTop = NULL;
  195.     }
  196.  
  197.     return hWnd;    // return the owner as HWND
  198. }
  199.  
  200. /////////////////////////////////////////////////////////////////////////////
  201. // UI related CCmdTarget functions
  202.  
  203. CView* PASCAL CCmdTarget::GetRoutingView_()
  204. {
  205.     CView* pView = AfxGetThreadState()->m_pRoutingView;
  206.     if (pView != NULL)
  207.         ASSERT_VALID(pView);
  208.     return pView;
  209. }
  210.  
  211. CFrameWnd* PASCAL CCmdTarget::GetRoutingFrame_()
  212. {
  213.     CFrameWnd* pFrame = AfxGetThreadState()->m_pRoutingFrame;
  214.     if (pFrame != NULL)
  215.         ASSERT_VALID(pFrame);
  216.     return pFrame;
  217. }
  218.  
  219. /////////////////////////////////////////////////////////////////////////////
  220.