home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / occdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  15.9 KB  |  630 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. #include "occimpl.h"
  13.  
  14. #ifdef AFX_OCC_SEG
  15. #pragma code_seg(AFX_OCC_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. #define new DEBUG_NEW
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26.  
  27. #define CH_SYSMENU ' '
  28.  
  29. #define WS_TYPEMASK 0xC0000000
  30. #define BS_TYPEMASK 0x0000000F
  31.  
  32. static inline DWORD TestStyle(CWnd* pWnd, DWORD dwStyle)
  33.     { return GetWindowLong(pWnd->m_hWnd, GWL_STYLE) & dwStyle; }
  34.  
  35. static inline DWORD TestExStyle(CWnd* pWnd, DWORD dwExStyle)
  36.     { return GetWindowLong(pWnd->m_hWnd, GWL_EXSTYLE) & dwExStyle; }
  37.  
  38. static inline BOOL HasChildStyle(CWnd* pWnd)
  39.     { return TestStyle(pWnd, WS_TYPEMASK) == WS_CHILD; }
  40.  
  41. static inline BOOL IsControlParent(CWnd* pWnd)
  42.     { return TestExStyle(pWnd, WS_EX_CONTROLPARENT); }
  43.  
  44. AFX_STATIC DWORD AFXAPI _AfxGetDlgCode(CWnd* pWnd, LPMSG lpMsg=NULL)
  45. {
  46.     if (pWnd == NULL)
  47.         return 0;
  48.  
  49.     WPARAM wParam = (lpMsg == NULL) ? 0 : lpMsg->wParam;
  50.  
  51.     return (DWORD)SendMessage(pWnd->m_hWnd, WM_GETDLGCODE,
  52.         wParam, (LPARAM)(LPMSG)lpMsg);
  53. }
  54.  
  55. AFX_STATIC void AFXAPI _AfxDlgSetFocus(CWnd* pWnd)
  56. {
  57.     // Select all text in an edit control.
  58.     if (_AfxGetDlgCode(pWnd) & DLGC_HASSETSEL)
  59.         pWnd->SendMessage(EM_SETSEL, 0, -1);
  60.  
  61.     // Set focus as normal.
  62.     pWnd->SetFocus();
  63. }
  64.  
  65. AFX_STATIC CWnd* AFXAPI _AfxGetChildControl(CWnd* pWndRoot, CWnd* pWndChild)
  66. {
  67.     CWnd* pWndControl = NULL;
  68.  
  69.     while ((pWndChild != NULL) && HasChildStyle(pWndChild) &&
  70.         (pWndChild != pWndRoot))
  71.     {
  72.         pWndControl = pWndChild;
  73.         pWndChild = pWndChild->GetParent();
  74.  
  75.         if (IsControlParent(pWndChild))
  76.             break;
  77.     }
  78.     return pWndControl;
  79. }
  80.  
  81. AFX_STATIC CWnd* AFXAPI _AfxNextControl(CWnd* pWndRoot, CWnd* pWndStart, UINT uFlags)
  82. {
  83.     // if pWndStart is already equal to pWndRoot, this confuses this function
  84.     // badly.
  85.     ASSERT(pWndRoot != pWndStart);
  86.  
  87.     if (pWndStart == NULL)
  88.     {
  89. FirstChild:
  90.         pWndStart = pWndRoot->GetTopWindow();
  91.         if (pWndStart == NULL)
  92.             return pWndRoot;
  93.  
  94.         goto Found;
  95.     }
  96.     else
  97.     {
  98.         // Are we at the last control within some parent?  If so, pop back up.
  99.         while (pWndStart->GetNextWindow() == NULL)
  100.         {
  101.             // Popup to previous real ancestor.  pWndStart will be NULL,
  102.             // pWndRoot, or the child of a recursive dialog.
  103.             pWndStart = _AfxGetChildControl(pWndRoot, pWndStart->GetParent());
  104.             if ((pWndStart == NULL) || (pWndStart == pWndRoot))
  105.             {
  106.                 goto FirstChild;
  107.             }
  108.         }
  109.  
  110.         ASSERT(pWndStart != NULL);
  111.         pWndStart = pWndStart->GetNextWindow();
  112.     }
  113.  
  114. Found:
  115.     if (IsControlParent(pWndStart))
  116.     {
  117.         if (((uFlags & CWP_SKIPINVISIBLE) && !pWndStart->IsWindowVisible()) ||
  118.             ((uFlags & CWP_SKIPDISABLED) && !pWndStart->IsWindowEnabled()))
  119.             pWndStart = _AfxNextControl(pWndRoot, pWndStart, uFlags);
  120.         else
  121.             pWndStart = _AfxNextControl(pWndStart, NULL, uFlags);
  122.     }
  123.  
  124.     return pWndStart;
  125. }
  126.  
  127. BOOL AFX_CDECL COccManager::IsMatchingMnemonic(CWnd* pWnd, LPMSG lpMsg)
  128. {
  129.     return (pWnd->m_pCtrlSite != NULL) &&
  130.         pWnd->m_pCtrlSite->IsMatchingMnemonic(lpMsg);
  131. }
  132.  
  133. AFX_STATIC CWnd* AFXAPI _AfxFindNextMnem(CWnd* pWndDlg, CWnd* pWnd, LPMSG lpMsg)
  134. {
  135.     CWnd* pWndStart;
  136.     CWnd* pWndT;
  137.     int i = 0;
  138.  
  139.     // Check if we are in a group box so we can find local mnemonics.
  140.     pWndStart = _AfxGetChildControl(pWndDlg, pWnd);
  141.  
  142.     while ((pWndT = pWndDlg->GetNextDlgGroupItem(pWndStart)) != NULL)
  143.     {
  144.         i++;
  145.  
  146.         // Avoid infinite looping.
  147.         if (pWndT == pWnd || i > 60)
  148.             break;
  149.  
  150.         pWndStart = pWndT;
  151.  
  152.         if (COccManager::IsMatchingMnemonic(pWndT, lpMsg))
  153.             return pWndT;
  154.     }
  155.  
  156.     pWnd = pWndStart = _AfxGetChildControl(pWndDlg, pWnd);
  157.  
  158.     while (TRUE)
  159.     {
  160.         pWnd = _AfxNextControl(pWndDlg, pWnd, CWP_SKIPINVISIBLE | CWP_SKIPDISABLED);
  161.  
  162.         if (COccManager::IsMatchingMnemonic(pWnd, lpMsg))
  163.             break;
  164.  
  165.         if (pWnd == pWndStart)
  166.             return NULL;
  167.     }
  168.  
  169.     return pWnd;
  170. }
  171.  
  172. BOOL AFX_CDECL COccManager::IsLabelControl(CWnd* pWnd)
  173. {
  174.     return pWnd->IsWindowEnabled() && (pWnd->m_pCtrlSite != NULL) &&
  175.         pWnd->m_pCtrlSite->m_dwMiscStatus & OLEMISC_ACTSLIKELABEL;
  176. }
  177.  
  178. AFX_STATIC CWnd* AFXAPI _AfxGetNextMnem(CWnd* pWndDlg, CWnd* pWnd, LPMSG lpMsg)
  179. {
  180.     CWnd* pWndFirstFound = NULL;
  181.  
  182.     // Loop for a long time but not long enough so we hang...
  183.     for (int count = 0; count < 256*2; count++)
  184.     {
  185.         // If the dialog box doesn't has the mnemonic specified, return NULL.
  186.         if ((pWnd = _AfxFindNextMnem(pWndDlg, pWnd, lpMsg)) == NULL)
  187.             return NULL;
  188.  
  189.         // If a non-disabled static item, then jump ahead to nearest tabstop.
  190.         if (COccManager::IsLabelControl(pWnd))
  191.         {
  192.             pWnd = pWndDlg->GetNextDlgTabItem(pWnd);
  193.             if (pWnd == NULL)
  194.                 return NULL;
  195.         }
  196.  
  197.         if (pWnd->IsWindowEnabled())
  198.             return pWnd;
  199.  
  200.         // Stop if we've looped back to the first item we checked
  201.         if (pWnd == pWndFirstFound)
  202.             return NULL;
  203.  
  204.         if (pWndFirstFound == NULL)
  205.             pWndFirstFound = pWnd;
  206.     }
  207.  
  208.     return NULL;
  209. }
  210.  
  211. void AFX_CDECL COccManager::UIActivateControl(CWnd* pWndNewFocus)
  212. {
  213.     if (pWndNewFocus == NULL)
  214.         return;
  215.  
  216.     // Find the nearest control in the window parent chain.
  217.     CWnd* pWndCtrl = pWndNewFocus;
  218.     COleControlContainer* pCtrlCont = NULL;
  219.     COleControlSite* pCtrlSite = NULL;
  220.     while ((pWndCtrl != NULL) &&
  221.         ((pCtrlCont = pWndCtrl->m_pCtrlCont) == NULL) &&
  222.         ((pCtrlSite = pWndCtrl->m_pCtrlSite) == NULL))
  223.     {
  224.         pWndCtrl = pWndCtrl->GetParent();
  225.     }
  226.  
  227.     if ((pWndCtrl == NULL) || (pCtrlCont != NULL))
  228.         return;
  229.  
  230.     // This will UI Activate the control.
  231.     pCtrlSite->SetFocus();
  232.  
  233.     // Make sure focus gets set to correct child of control, if any.
  234.     if (CWnd::GetFocus() != pWndNewFocus)
  235.         pWndNewFocus->SetFocus();
  236. }
  237.  
  238. void AFX_CDECL COccManager::UIDeactivateIfNecessary(CWnd* pWndOldFocus,
  239.     CWnd* pWndNewFocus)
  240. {
  241.     if (pWndOldFocus == NULL || !::IsWindow(pWndOldFocus->m_hWnd))
  242.         return;
  243.  
  244.     // Find the nearest control container in the window parent chain.
  245.     CWnd* pWndCtrlCont = pWndOldFocus->GetParent();
  246.     COleControlContainer* pCtrlCont = NULL;
  247.  
  248.     while ((pWndCtrlCont != NULL) &&
  249.         ((pCtrlCont = pWndCtrlCont->m_pCtrlCont) == NULL))
  250.     {
  251.         pWndCtrlCont = pWndCtrlCont->GetParent();
  252.     }
  253.  
  254.     if (pCtrlCont == NULL)
  255.         return;
  256.  
  257.     // Get the current UI Active control (if any).
  258.     CWnd* pWndUIActive = NULL;
  259.     COleControlSite* pSite = NULL;
  260.  
  261.     if ((pCtrlCont != NULL) &&
  262.         ((pSite = pCtrlCont->m_pSiteUIActive) != NULL))
  263.     {
  264.         pWndUIActive = CWnd::FromHandle(pSite->m_hWnd);
  265.     }
  266.  
  267.     // Ignore if no control is UI Active.
  268.     if (pWndUIActive == NULL)
  269.         return;
  270.  
  271.     // Ignore if the control getting the focus is the same control.
  272.     if ((pWndNewFocus == pWndUIActive) ||
  273.         ((pWndNewFocus != NULL) && pWndUIActive->IsChild(pWndNewFocus)))
  274.         return;
  275.  
  276.     // Tell the container to UI Deactivate the UI Active control.
  277.     pCtrlCont->OnUIActivate(NULL);
  278. }
  279.  
  280. CWnd* AFXAPI _AfxFindDlgItem(CWnd* pWndParent, DWORD id)
  281. {
  282.     CWnd* pWndChild;
  283.     CWnd* pWndOrig;
  284.  
  285.     // QUICK TRY:
  286.     pWndChild = pWndParent->GetDlgItem(id);
  287.     if (pWndChild != NULL)
  288.         return pWndChild;
  289.  
  290.     pWndOrig = _AfxNextControl(pWndParent, NULL, CWP_SKIPINVISIBLE);
  291.     if (pWndOrig == pWndParent)
  292.         return NULL;
  293.  
  294.     pWndChild = pWndOrig;
  295.  
  296.     do
  297.     {
  298.         if ((DWORD)pWndChild->GetDlgCtrlID() == id)
  299.             return(pWndChild);
  300.  
  301.         pWndChild = _AfxNextControl(pWndParent, pWndChild, CWP_SKIPINVISIBLE);
  302.     }
  303.     while ((pWndChild != NULL) && (pWndChild != pWndOrig));
  304.  
  305.     return NULL;
  306. }
  307.  
  308. void COccManager::SetDefaultButton(CWnd* pWnd, BOOL bDefault)
  309. {
  310.     if (pWnd->m_pCtrlSite != NULL)
  311.     {
  312.         pWnd->m_pCtrlSite->SetDefaultButton(bDefault);
  313.     }
  314.     else
  315.     {
  316.         DWORD code = _AfxGetDlgCode(pWnd);
  317.         if (code & (bDefault ? DLGC_UNDEFPUSHBUTTON : DLGC_DEFPUSHBUTTON))
  318.             pWnd->SendMessage(BM_SETSTYLE,
  319.                 (WPARAM)(bDefault ? BS_DEFPUSHBUTTON : BS_PUSHBUTTON),
  320.                 (LPARAM)(DWORD)TRUE);
  321.     }
  322. }
  323.  
  324. DWORD AFX_CDECL COccManager::GetDefBtnCode(CWnd* pWnd)
  325. {
  326.     if (pWnd == NULL)
  327.         return 0;
  328.  
  329.     if (pWnd->m_pCtrlSite != NULL)
  330.         return pWnd->m_pCtrlSite->GetDefBtnCode();
  331.  
  332.     return _AfxGetDlgCode(pWnd) & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON);
  333. }
  334.  
  335. AFX_STATIC void AFXAPI _AfxRemoveDefaultButton(CWnd* pWndRoot, CWnd* pWndStart)
  336. {
  337.     if ((pWndStart == NULL) || IsControlParent(pWndStart))
  338.         pWndStart = _AfxNextControl(pWndRoot, NULL, CWP_SKIPINVISIBLE | CWP_SKIPDISABLED);
  339.     else
  340.         pWndStart = _AfxGetChildControl(pWndRoot, pWndStart);
  341.  
  342.     if (pWndStart == NULL)
  343.         return;
  344.  
  345.     CWnd* pWnd = pWndStart;
  346.     CWnd* pWndNext;
  347.  
  348.     do
  349.     {
  350.         COccManager::SetDefaultButton(pWnd, FALSE);
  351.         pWndNext = _AfxNextControl(pWndRoot, pWnd, 0);
  352.         pWnd = pWndNext;
  353.     }
  354.     while ((pWnd != NULL) && (pWnd != pWndStart));
  355. }
  356.  
  357. AFX_STATIC int AFXAPI _AfxOriginalDefButton(CWnd* pWndRoot)
  358. {
  359.     LRESULT lResult = pWndRoot->SendMessage(DM_GETDEFID, 0, 0L);
  360.     return HIWORD(lResult) == DC_HASDEFID ? LOWORD(lResult) : IDOK;
  361. }
  362.  
  363. AFX_STATIC void AFXAPI _AfxCheckDefPushButton(CWnd* pWndRoot, CWnd* pWndOldFocus,
  364.     CWnd* pWndNewFocus)
  365. {
  366.     DWORD code = 0;
  367.     CWnd* pWndT;
  368.  
  369.     // If the focus has gone to a totally separate window, bail out.
  370.     if (!pWndRoot->IsChild(pWndNewFocus))
  371.         return;
  372.  
  373.     if (pWndNewFocus != NULL)
  374.     {
  375.         // Do nothing if clicking on dialog background or recursive dialog
  376.         // background.
  377.         if (IsControlParent(pWndNewFocus))
  378.             return;
  379.  
  380.         code = COccManager::GetDefBtnCode(pWndNewFocus);
  381.     }
  382.  
  383.     if (pWndOldFocus == pWndNewFocus)
  384.     {
  385.         // Check the default ID and see if is the same as pwndOldFocus' ID.
  386.         // If not, find it and use it as pwndOldFocus
  387.         if (code & DLGC_UNDEFPUSHBUTTON)
  388.         {
  389.             if (pWndOldFocus != NULL)
  390.             {
  391.                 pWndOldFocus = _AfxFindDlgItem(pWndRoot, _AfxOriginalDefButton(pWndRoot));
  392.                 if ((pWndOldFocus != NULL) && (pWndOldFocus != pWndNewFocus))
  393.                 {
  394.                     if (COccManager::GetDefBtnCode(pWndOldFocus) & DLGC_DEFPUSHBUTTON)
  395.                     {
  396.                         _AfxRemoveDefaultButton(pWndRoot, pWndOldFocus);
  397.                         goto SetNewDefault;
  398.                     }
  399.                 }
  400.             }
  401.  
  402.             COccManager::SetDefaultButton(pWndNewFocus, TRUE);
  403.         }
  404.         return;
  405.     }
  406.  
  407.     // If the focus is changing to or from a pushbutton, then remove the
  408.     // default style from the current default button
  409.     if (((pWndOldFocus != NULL) && (COccManager::GetDefBtnCode(pWndOldFocus) != 0)) ||
  410.         ((pWndNewFocus != NULL) && (code != 0)))
  411.     {
  412.         _AfxRemoveDefaultButton(pWndRoot, pWndNewFocus);
  413.     }
  414.  
  415. SetNewDefault:
  416.     // If moving to a button, make that button the default.
  417.     if (code & (DLGC_UNDEFPUSHBUTTON | DLGC_DEFPUSHBUTTON))
  418.     {
  419.         COccManager::SetDefaultButton(pWndNewFocus, TRUE);
  420.     }
  421.     else
  422.     {
  423.         // Otherwise, make sure the original default button is default
  424.  
  425.         // Get the original default button
  426.         pWndT = _AfxFindDlgItem(pWndRoot, _AfxOriginalDefButton(pWndRoot));
  427.  
  428.         if ((COccManager::GetDefBtnCode(pWndT) & DLGC_UNDEFPUSHBUTTON) &&
  429.             pWndT->IsWindowEnabled())
  430.         {
  431.             COccManager::SetDefaultButton(pWndT, TRUE);
  432.         }
  433.     }
  434. }
  435.  
  436. BOOL COccManager::IsDialogMessage(CWnd* pWndDlg, LPMSG lpMsg)
  437. {
  438.     // If an OLE Control has the focus, then give it the first crack at key
  439.     // and mouse messages.
  440.  
  441.     CWnd* pWndFocus = CWnd::GetFocus();
  442.     HWND hWndFocus = pWndFocus->GetSafeHwnd();
  443.     HWND hWndDlg = pWndDlg->GetSafeHwnd();
  444.     UINT uMsg = lpMsg->message;
  445.  
  446.     if (((uMsg >= WM_KEYFIRST) && (uMsg <= WM_KEYLAST)) ||
  447.         ((uMsg >= WM_MOUSEFIRST) && (uMsg <= WM_MOUSELAST)))
  448.     {
  449.         CWnd* pWndCtrl = pWndFocus;
  450.  
  451.         // Walk up the parent chain, until we find an OLE control.
  452.         while ((pWndCtrl != NULL) && (pWndCtrl->m_pCtrlSite == NULL) &&
  453.             (pWndCtrl->GetParent() != pWndDlg))
  454.         {
  455.             pWndCtrl = pWndCtrl->GetParent();
  456.         }
  457.  
  458.         // let the control attempt to translate the message
  459.         if (pWndCtrl != NULL && pWndCtrl->m_pCtrlSite != NULL &&
  460.             pWndCtrl->m_pCtrlSite->m_pActiveObject != NULL &&
  461.             pWndCtrl->m_pCtrlSite->m_pActiveObject->TranslateAccelerator(lpMsg) == S_OK)
  462.         {
  463.             return TRUE;
  464.         }
  465.  
  466.         // handle CTRLINFO_EATS_RETURN and CTRLINFO_EATS_ESCAPE flags
  467.         if ((uMsg == WM_KEYUP || uMsg == WM_KEYDOWN || uMsg == WM_CHAR) &&
  468.             pWndCtrl != NULL && pWndCtrl->m_pCtrlSite != NULL &&
  469.             ((LOWORD(lpMsg->wParam) == VK_RETURN &&
  470.              (pWndCtrl->m_pCtrlSite->m_ctlInfo.dwFlags & CTRLINFO_EATS_RETURN)) ||
  471.             (LOWORD(lpMsg->wParam) == VK_ESCAPE &&
  472.              (pWndCtrl->m_pCtrlSite->m_ctlInfo.dwFlags & CTRLINFO_EATS_ESCAPE))))
  473.         {
  474.             return FALSE;
  475.         }
  476.     }
  477.  
  478.     BOOL bResult = FALSE;
  479.     CWnd* pWndMsg = CWnd::FromHandle(lpMsg->hwnd);
  480.     CWnd* pWndNext = NULL;
  481.     DWORD code;
  482.     BOOL bBack = FALSE;
  483.     int iOK = IDCANCEL;
  484.  
  485.     switch (uMsg)
  486.     {
  487.     case WM_SYSCHAR:
  488.         // If no control has focus, and Alt not down, then ignore.
  489.         if ((pWndFocus == NULL) && (GetKeyState(VK_MENU) >= 0))
  490.             break;
  491.  
  492.         // If alt+menuchar, process as menu.
  493.         if (LOWORD(lpMsg->wParam) == CH_SYSMENU)
  494.             break;
  495.         // FALL THRU
  496.  
  497.     case WM_CHAR:
  498.         // Ignore chars sent to the dialog box (rather than the control).
  499.         if (pWndMsg == pWndDlg)
  500.             return TRUE;
  501.  
  502.         code = _AfxGetDlgCode(pWndMsg, lpMsg);
  503.  
  504.         // If the control wants to process the message, then don't check
  505.         // for possible mnemonic key.
  506.         if (uMsg == WM_CHAR && (code & (DLGC_WANTCHARS|DLGC_WANTMESSAGE)))
  507.             break;
  508.  
  509.         // If the control wants tabs, then don't let tab fall thru here
  510.         if (LOWORD(lpMsg->wParam) == VK_TAB && (code & DLGC_WANTTAB))
  511.             break;
  512.  
  513.         // Don't handle space as a mnemonic
  514.         if (LOWORD(lpMsg->wParam) == VK_SPACE)
  515.             return TRUE;
  516.  
  517.         if ((pWndNext = _AfxGetNextMnem(pWndDlg, pWndMsg, lpMsg)) != NULL)
  518.         {
  519.             if (pWndNext->m_pCtrlSite != NULL)
  520.             {
  521.                 // UI Activate new control, and send the mnemonic to it.
  522.                 pWndNext->m_pCtrlSite->SendMnemonic(lpMsg);
  523.                 bResult = TRUE;
  524.             }
  525.         }
  526.         break;
  527.  
  528.     case WM_KEYDOWN:
  529.         code = _AfxGetDlgCode(pWndMsg, lpMsg);
  530.         switch (LOWORD(lpMsg->wParam))
  531.         {
  532.         case VK_TAB:
  533.             if (code & DLGC_WANTTAB)    // If control wants tabs, bail out.
  534.                 break;
  535.  
  536.             pWndNext = pWndDlg->GetNextDlgTabItem(pWndMsg,
  537.                 (GetKeyState(VK_SHIFT) < 0));
  538.  
  539.             if (pWndNext != NULL)
  540.             {
  541.                 _AfxDlgSetFocus(pWndNext);
  542.                 UIDeactivateIfNecessary(pWndFocus, pWndNext);
  543.             }
  544.  
  545.             bResult = TRUE;
  546.             break;
  547.  
  548.         case VK_LEFT:
  549.         case VK_UP:
  550.             bBack = TRUE;
  551.             // FALL THRU
  552.  
  553.         case VK_RIGHT:
  554.         case VK_DOWN:
  555.             if (_AfxGetDlgCode(pWndFocus, lpMsg) & DLGC_WANTARROWS)
  556.                 break;
  557.  
  558.             pWndNext = pWndDlg->GetNextDlgGroupItem(pWndFocus, bBack);
  559.             if ((pWndNext != NULL) && (pWndNext->m_pCtrlSite != NULL))
  560.             {
  561.                 _AfxDlgSetFocus(pWndNext);
  562.                 bResult = TRUE;
  563.             }
  564.             break;
  565.  
  566.         case VK_EXECUTE:
  567.         case VK_RETURN:
  568.             // Return was pressed. Find default button and click it.
  569.             if (GetDefBtnCode(pWndFocus) & DLGC_DEFPUSHBUTTON)
  570.             {
  571.                 pWndNext = pWndFocus;
  572.                 iOK = (DWORD)pWndNext->GetDlgCtrlID();
  573.             }
  574.             else
  575.             {
  576.                 iOK = _AfxOriginalDefButton(pWndDlg);
  577.             }
  578.             // FALL THRU
  579.  
  580.         case VK_ESCAPE:
  581.         case VK_CANCEL:
  582.             if (pWndNext == NULL)
  583.             {
  584.                 pWndNext = _AfxFindDlgItem(pWndDlg, iOK);
  585.                 if (pWndNext == NULL)
  586.                     break;
  587.             }
  588.             ASSERT(pWndNext != NULL);
  589.  
  590.             // Make sure button is not disabled.
  591.             if (!pWndNext->IsWindowEnabled())
  592.             {
  593.                 MessageBeep(0);
  594.             }
  595.             else if (pWndNext->m_pCtrlSite != NULL)
  596.             {
  597.                 // "push" the pWndNext control.
  598.                 TRY
  599.                 {
  600.                     pWndNext->InvokeHelper(DISPID_DOCLICK, DISPATCH_METHOD,
  601.                         VT_EMPTY, NULL, VTS_NONE);
  602.                 }
  603.                 END_TRY
  604.                 bResult = TRUE;
  605.             }
  606.             break;
  607.         }
  608.         break;
  609.     }
  610.  
  611.     // As a last resort, delegate to the Windows implementation
  612.     if (!bResult)
  613.     {
  614.         bResult = ::IsDialogMessage(pWndDlg->m_hWnd, lpMsg);
  615.         if (bResult && (CWnd::GetFocus() != pWndFocus))
  616.             UIActivateControl(CWnd::GetFocus());
  617.     }
  618.  
  619.     if (::IsWindow(hWndFocus))
  620.     {
  621.         UIDeactivateIfNecessary(pWndFocus, CWnd::GetFocus());
  622.         if (::IsWindow(hWndDlg))
  623.             _AfxCheckDefPushButton(pWndDlg, pWndFocus, CWnd::GetFocus());
  624.     }
  625.  
  626.     return bResult;
  627. }
  628.  
  629. /////////////////////////////////////////////////////////////////////////////
  630.