home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / DLGDATA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  16.8 KB  |  621 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. #include "occimpl.h"
  13.  
  14. #ifdef AFX_CORE3_SEG
  15. #pragma code_seg(AFX_CORE3_SEG)
  16. #endif
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CDataExchange member functions (contructor is in wincore.cpp for swap tuning)
  25.  
  26. HWND CDataExchange::PrepareEditCtrl(int nIDC)
  27. {
  28.     HWND hWndCtrl = PrepareCtrl(nIDC);
  29.     ASSERT(hWndCtrl != NULL);
  30.     m_bEditLastControl = TRUE;
  31.     return hWndCtrl;
  32. }
  33.  
  34. HWND CDataExchange::PrepareCtrl(int nIDC)
  35. {
  36.     ASSERT(nIDC != 0);
  37.     ASSERT(nIDC != -1); // not allowed
  38.     HWND hWndCtrl;
  39.     m_pDlgWnd->GetDlgItem(nIDC, &hWndCtrl);
  40.     if (hWndCtrl == NULL)
  41.     {
  42.         TRACE1("Error: no data exchange control with ID 0x%04X.\n", nIDC);
  43.         ASSERT(FALSE);
  44.         AfxThrowNotSupportedException();
  45.     }
  46.     m_hWndLastControl = hWndCtrl;
  47.     m_bEditLastControl = FALSE; // not an edit item by default
  48.     ASSERT(hWndCtrl != NULL);   // never return NULL handle
  49.     return hWndCtrl;
  50. }
  51.  
  52. void CDataExchange::Fail()
  53. {
  54.     if (!m_bSaveAndValidate)
  55.     {
  56.         TRACE0("Warning: CDataExchange::Fail called when not validating.\n");
  57.         // throw the exception anyway
  58.     }
  59.     else if (m_hWndLastControl != NULL)
  60.     {
  61.         // restore focus and selection to offending field
  62.         ::SetFocus(m_hWndLastControl);
  63.         if (m_bEditLastControl) // select edit item
  64.             ::SendMessage(m_hWndLastControl, EM_SETSEL, 0, -1);
  65.     }
  66.     else
  67.     {
  68.         TRACE0("Error: fail validation with no control to restore focus to.\n");
  69.         // do nothing more
  70.     }
  71.  
  72.     AfxThrowUserException();
  73. }
  74.  
  75. /////////////////////////////////////////////////////////////////////////////
  76. // Notes for implementing dialog data exchange and validation procs:
  77. //  * always start with PrepareCtrl or PrepareEditCtrl
  78. //  * always start with 'pDX->m_bSaveAndValidate' check
  79. //  * pDX->Fail() will throw an exception - so be prepared
  80. //  * avoid creating temporary HWNDs for dialog controls - i.e.
  81. //      use HWNDs for child elements
  82. //  * validation procs should only act if 'm_bSaveAndValidate'
  83. //  * use the suffices:
  84. //      DDX_ = exchange proc
  85. //      DDV_ = validation proc
  86. //
  87. /////////////////////////////////////////////////////////////////////////////
  88.  
  89. // only supports '%d', '%u', '%sd', '%su', '%ld' and '%lu'
  90. static BOOL AFXAPI AfxSimpleScanf(LPCTSTR lpszText,
  91.     LPCTSTR lpszFormat, va_list pData)
  92. {
  93.     ASSERT(lpszText != NULL);
  94.     ASSERT(lpszFormat != NULL);
  95.  
  96.     ASSERT(*lpszFormat == '%');
  97.     lpszFormat++;        // skip '%'
  98.  
  99.     BOOL bLong = FALSE;
  100.     BOOL bShort = FALSE;
  101.     if (*lpszFormat == 'l')
  102.     {
  103.         bLong = TRUE;
  104.         lpszFormat++;
  105.     }
  106.     else if (*lpszFormat == 's')
  107.     {
  108.         bShort = TRUE;
  109.         lpszFormat++;
  110.     }
  111.  
  112.     ASSERT(*lpszFormat == 'd' || *lpszFormat == 'u');
  113.     ASSERT(lpszFormat[1] == '\0');
  114.  
  115.     while (*lpszText == ' ' || *lpszText == '\t')
  116.         lpszText++;
  117.     TCHAR chFirst = lpszText[0];
  118.     long l, l2;
  119.     if (*lpszFormat == 'd')
  120.     {
  121.         // signed
  122.         l = _tcstol(lpszText, (LPTSTR*)&lpszText, 10);
  123.         l2 = (int)l;
  124.     }
  125.     else
  126.     {
  127.         // unsigned
  128.         if (*lpszText == '-')
  129.             return FALSE;
  130.         l = (long)_tcstoul(lpszText, (LPTSTR*)&lpszText, 10);
  131.         l2 = (unsigned int)l;
  132.     }
  133.     if (l == 0 && chFirst != '0')
  134.         return FALSE;   // could not convert
  135.  
  136.     while (*lpszText == ' ' || *lpszText == '\t')
  137.         lpszText++;
  138.     if (*lpszText != '\0')
  139.         return FALSE;   // not terminated properly
  140.  
  141.     if (bShort)
  142.     {
  143.         if ((short)l != l)
  144.             return FALSE;   // too big for short
  145.         *va_arg(pData, short*) = (short)l;
  146.     }
  147.     else
  148.     {
  149.         ASSERT(sizeof(long) == sizeof(int));
  150.         ASSERT(l == l2);
  151.         *va_arg(pData, long*) = l;
  152.     }
  153.  
  154.     // all ok
  155.     return TRUE;
  156. }
  157.  
  158. static void AFX_CDECL DDX_TextWithFormat(CDataExchange* pDX, int nIDC,
  159.     LPCTSTR lpszFormat, UINT nIDPrompt, ...)
  160.     // only supports windows output formats - no floating point
  161. {
  162.     va_list pData;
  163.     va_start(pData, nIDPrompt);
  164.  
  165.     HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  166.     TCHAR szT[32];
  167.     if (pDX->m_bSaveAndValidate)
  168.     {
  169.         // the following works for %d, %u, %ld, %lu
  170.         ::GetWindowText(hWndCtrl, szT, _countof(szT));
  171.         if (!AfxSimpleScanf(szT, lpszFormat, pData))
  172.         {
  173.             AfxMessageBox(nIDPrompt);
  174.             pDX->Fail();        // throws exception
  175.         }
  176.     }
  177.     else
  178.     {
  179.         wvsprintf(szT, lpszFormat, pData);
  180.             // does not support floating point numbers - see dlgfloat.cpp
  181.         AfxSetWindowText(hWndCtrl, szT);
  182.     }
  183.  
  184.     va_end(pData);
  185. }
  186.  
  187. /////////////////////////////////////////////////////////////////////////////
  188. // Simple formatting to text item
  189.  
  190. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, BYTE& value)
  191. {
  192.     int n = (int)value;
  193.     if (pDX->m_bSaveAndValidate)
  194.     {
  195.         DDX_TextWithFormat(pDX, nIDC, _T("%u"), AFX_IDP_PARSE_BYTE, &n);
  196.         if (n > 255)
  197.         {
  198.             AfxMessageBox(AFX_IDP_PARSE_BYTE);
  199.             pDX->Fail();        // throws exception
  200.         }
  201.         value = (BYTE)n;
  202.     }
  203.     else
  204.         DDX_TextWithFormat(pDX, nIDC, _T("%u"), AFX_IDP_PARSE_BYTE, n);
  205. }
  206.  
  207. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, short& value)
  208. {
  209.     if (pDX->m_bSaveAndValidate)
  210.         DDX_TextWithFormat(pDX, nIDC, _T("%sd"), AFX_IDP_PARSE_INT, &value);
  211.     else
  212.         DDX_TextWithFormat(pDX, nIDC, _T("%hd"), AFX_IDP_PARSE_INT, value);
  213. }
  214.  
  215. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, int& value)
  216. {
  217.     if (pDX->m_bSaveAndValidate)
  218.         DDX_TextWithFormat(pDX, nIDC, _T("%d"), AFX_IDP_PARSE_INT, &value);
  219.     else
  220.         DDX_TextWithFormat(pDX, nIDC, _T("%d"), AFX_IDP_PARSE_INT, value);
  221. }
  222.  
  223. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, UINT& value)
  224. {
  225.     if (pDX->m_bSaveAndValidate)
  226.         DDX_TextWithFormat(pDX, nIDC, _T("%u"), AFX_IDP_PARSE_UINT, &value);
  227.     else
  228.         DDX_TextWithFormat(pDX, nIDC, _T("%u"), AFX_IDP_PARSE_UINT, value);
  229. }
  230.  
  231. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, long& value)
  232. {
  233.     if (pDX->m_bSaveAndValidate)
  234.         DDX_TextWithFormat(pDX, nIDC, _T("%ld"), AFX_IDP_PARSE_INT, &value);
  235.     else
  236.         DDX_TextWithFormat(pDX, nIDC, _T("%ld"), AFX_IDP_PARSE_INT, value);
  237. }
  238.  
  239. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, DWORD& value)
  240. {
  241.     if (pDX->m_bSaveAndValidate)
  242.         DDX_TextWithFormat(pDX, nIDC, _T("%lu"), AFX_IDP_PARSE_UINT, &value);
  243.     else
  244.         DDX_TextWithFormat(pDX, nIDC, _T("%lu"), AFX_IDP_PARSE_UINT, value);
  245. }
  246.  
  247. void AFXAPI DDX_Text(CDataExchange* pDX, int nIDC, CString& value)
  248. {
  249.     HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  250.     if (pDX->m_bSaveAndValidate)
  251.     {
  252.         int nLen = ::GetWindowTextLength(hWndCtrl);
  253.         ::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1);
  254.         value.ReleaseBuffer();
  255.     }
  256.     else
  257.     {
  258.         AfxSetWindowText(hWndCtrl, value);
  259.     }
  260. }
  261.  
  262. /////////////////////////////////////////////////////////////////////////////
  263. // Data exchange for special control
  264.  
  265. void AFXAPI DDX_Check(CDataExchange* pDX, int nIDC, int& value)
  266. {
  267.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  268.     if (pDX->m_bSaveAndValidate)
  269.     {
  270.         value = (int)::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L);
  271.         ASSERT(value >= 0 && value <= 2);
  272.     }
  273.     else
  274.     {
  275.         if (value < 0 || value > 2)
  276.         {
  277.             TRACE1("Warning: dialog data checkbox value (%d) out of range.\n",
  278.                  value);
  279.             value = 0;  // default to off
  280.         }
  281.         ::SendMessage(hWndCtrl, BM_SETCHECK, (WPARAM)value, 0L);
  282.     }
  283. }
  284.  
  285. void AFXAPI DDX_Radio(CDataExchange* pDX, int nIDC, int& value)
  286.     // must be first in a group of auto radio buttons
  287. {
  288.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  289.  
  290.     ASSERT(::GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP);
  291.     ASSERT(::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON);
  292.  
  293.     if (pDX->m_bSaveAndValidate)
  294.         value = -1;     // value if none found
  295.  
  296.     // walk all children in group
  297.     int iButton = 0;
  298.     do
  299.     {
  300.         if (::SendMessage(hWndCtrl, WM_GETDLGCODE, 0, 0L) & DLGC_RADIOBUTTON)
  301.         {
  302.             // control in group is a radio button
  303.             if (pDX->m_bSaveAndValidate)
  304.             {
  305.                 if (::SendMessage(hWndCtrl, BM_GETCHECK, 0, 0L) != 0)
  306.                 {
  307.                     ASSERT(value == -1);    // only set once
  308.                     value = iButton;
  309.                 }
  310.             }
  311.             else
  312.             {
  313.                 // select button
  314.                 ::SendMessage(hWndCtrl, BM_SETCHECK, (iButton == value), 0L);
  315.             }
  316.             iButton++;
  317.         }
  318.         else
  319.         {
  320.             TRACE0("Warning: skipping non-radio button in group.\n");
  321.         }
  322.         hWndCtrl = ::GetWindow(hWndCtrl, GW_HWNDNEXT);
  323.  
  324.     } while (hWndCtrl != NULL &&
  325.         !(GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP));
  326. }
  327.  
  328. /////////////////////////////////////////////////////////////////////////////
  329. // Listboxes, comboboxes
  330.  
  331. void AFXAPI DDX_LBString(CDataExchange* pDX, int nIDC, CString& value)
  332. {
  333.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  334.     if (pDX->m_bSaveAndValidate)
  335.     {
  336.         int nIndex = (int)::SendMessage(hWndCtrl, LB_GETCURSEL, 0, 0L);
  337.         if (nIndex != -1)
  338.         {
  339.             int nLen = (int)::SendMessage(hWndCtrl, LB_GETTEXTLEN, nIndex, 0L);
  340.             ::SendMessage(hWndCtrl, LB_GETTEXT, nIndex,
  341.                     (LPARAM)(LPVOID)value.GetBufferSetLength(nLen));
  342.         }
  343.         else
  344.         {
  345.             // no selection
  346.             value.Empty();
  347.         }
  348.         value.ReleaseBuffer();
  349.     }
  350.     else
  351.     {
  352.         // set current selection based on data string
  353.         if (::SendMessage(hWndCtrl, LB_SELECTSTRING, (WPARAM)-1,
  354.           (LPARAM)(LPCTSTR)value) == LB_ERR)
  355.         {
  356.             // no selection match
  357.             TRACE0("Warning: no listbox item selected.\n");
  358.         }
  359.     }
  360. }
  361.  
  362. void AFXAPI DDX_LBStringExact(CDataExchange* pDX, int nIDC, CString& value)
  363. {
  364.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  365.     if (pDX->m_bSaveAndValidate)
  366.     {
  367.         DDX_LBString(pDX, nIDC, value);
  368.     }
  369.     else
  370.     {
  371.         // set current selection based on data string
  372.         int i = (int)::SendMessage(hWndCtrl, LB_FINDSTRINGEXACT, (WPARAM)-1,
  373.           (LPARAM)(LPCTSTR)value);
  374.         if (i < 0)
  375.         {
  376.             // no selection match
  377.             TRACE0("Warning: no listbox item selected.\n");
  378.         }
  379.         else
  380.         {
  381.             // select it
  382.             SendMessage(hWndCtrl, LB_SETCURSEL, i, 0L);
  383.         }
  384.     }
  385. }
  386.  
  387. void AFXAPI DDX_CBString(CDataExchange* pDX, int nIDC, CString& value)
  388. {
  389.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  390.     if (pDX->m_bSaveAndValidate)
  391.     {
  392.         // just get current edit item text (or drop list static)
  393.         int nLen = ::GetWindowTextLength(hWndCtrl);
  394.         if (nLen != -1)
  395.         {
  396.             // get known length
  397.             ::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1);
  398.         }
  399.         else
  400.         {
  401.             // for drop lists GetWindowTextLength does not work - assume
  402.             //  max of 255 characters
  403.             ::GetWindowText(hWndCtrl, value.GetBuffer(255), 255+1);
  404.         }
  405.         value.ReleaseBuffer();
  406.     }
  407.     else
  408.     {
  409.         // set current selection based on model string
  410.         if (::SendMessage(hWndCtrl, CB_SELECTSTRING, (WPARAM)-1,
  411.             (LPARAM)(LPCTSTR)value) == CB_ERR)
  412.         {
  413.             // just set the edit text (will be ignored if DROPDOWNLIST)
  414.             AfxSetWindowText(hWndCtrl, value);
  415.         }
  416.     }
  417. }
  418.  
  419. void AFXAPI DDX_CBStringExact(CDataExchange* pDX, int nIDC, CString& value)
  420. {
  421.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  422.     if (pDX->m_bSaveAndValidate)
  423.     {
  424.         DDX_CBString(pDX, nIDC, value);
  425.     }
  426.     else
  427.     {
  428.         // set current selection based on data string
  429.         int i = (int)::SendMessage(hWndCtrl, CB_FINDSTRINGEXACT, (WPARAM)-1,
  430.           (LPARAM)(LPCTSTR)value);
  431.         if (i < 0)
  432.         {
  433.             // no selection match
  434.             TRACE0("Warning: no combobox item selected.\n");
  435.         }
  436.         else
  437.         {
  438.             // select it
  439.             SendMessage(hWndCtrl, CB_SETCURSEL, i, 0L);
  440.         }
  441.     }
  442. }
  443.  
  444. void AFXAPI DDX_LBIndex(CDataExchange* pDX, int nIDC, int& index)
  445. {
  446.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  447.     if (pDX->m_bSaveAndValidate)
  448.         index = (int)::SendMessage(hWndCtrl, LB_GETCURSEL, 0, 0L);
  449.     else
  450.         ::SendMessage(hWndCtrl, LB_SETCURSEL, (WPARAM)index, 0L);
  451. }
  452.  
  453. void AFXAPI DDX_CBIndex(CDataExchange* pDX, int nIDC, int& index)
  454. {
  455.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  456.     if (pDX->m_bSaveAndValidate)
  457.         index = (int)::SendMessage(hWndCtrl, CB_GETCURSEL, 0, 0L);
  458.     else
  459.         ::SendMessage(hWndCtrl, CB_SETCURSEL, (WPARAM)index, 0L);
  460. }
  461.  
  462. void AFXAPI DDX_Scroll(CDataExchange* pDX, int nIDC, int& value)
  463. {
  464.     HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  465.     if (pDX->m_bSaveAndValidate)
  466.         value = GetScrollPos(hWndCtrl, SB_CTL);
  467.     else
  468.         SetScrollPos(hWndCtrl, SB_CTL, value, TRUE);
  469. }
  470. /////////////////////////////////////////////////////////////////////////////
  471. // Range Dialog Data Validation
  472.  
  473. static void AFXAPI FailMinMaxWithFormat(CDataExchange* pDX,
  474.      long minVal, long maxVal, LPCTSTR lpszFormat, UINT nIDPrompt)
  475.     // error string must have '%1' and '%2' strings for min and max values
  476.     // wsprintf formatting uses long values (format should be '%ld' or '%lu')
  477. {
  478.     ASSERT(lpszFormat != NULL);
  479.  
  480.     if (!pDX->m_bSaveAndValidate)
  481.     {
  482.         TRACE0("Warning: initial dialog data is out of range.\n");
  483.         return;     // don't stop now
  484.     }
  485.     TCHAR szMin[32];
  486.     TCHAR szMax[32];
  487.     wsprintf(szMin, lpszFormat, minVal);
  488.     wsprintf(szMax, lpszFormat, maxVal);
  489.     CString prompt;
  490.     AfxFormatString2(prompt, nIDPrompt, szMin, szMax);
  491.     AfxMessageBox(prompt, MB_ICONEXCLAMATION, nIDPrompt);
  492.     prompt.Empty(); // exception prep
  493.     pDX->Fail();
  494. }
  495.  
  496. //NOTE: don't use overloaded function names to avoid type ambiguities
  497. void AFXAPI DDV_MinMaxByte(CDataExchange* pDX, BYTE value, BYTE minVal, BYTE maxVal)
  498. {
  499.     ASSERT(minVal <= maxVal);
  500.     if (value < minVal || value > maxVal)
  501.         FailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%u"),
  502.             AFX_IDP_PARSE_INT_RANGE);
  503. }
  504.  
  505. void AFXAPI DDV_MinMaxShort(CDataExchange* pDX, short value, short minVal, short maxVal)
  506. {
  507.     ASSERT(minVal <= maxVal);
  508.     if (value < minVal || value > maxVal)
  509.         FailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%ld"),
  510.             AFX_IDP_PARSE_INT_RANGE);
  511. }
  512.  
  513. void AFXAPI DDV_MinMaxInt(CDataExchange* pDX, int value, int minVal, int maxVal)
  514. {
  515.     ASSERT(minVal <= maxVal);
  516.     if (value < minVal || value > maxVal)
  517.         FailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%ld"),
  518.             AFX_IDP_PARSE_INT_RANGE);
  519. }
  520.  
  521. void AFXAPI DDV_MinMaxLong(CDataExchange* pDX, long value, long minVal, long maxVal)
  522. {
  523.     ASSERT(minVal <= maxVal);
  524.     if (value < minVal || value > maxVal)
  525.         FailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%ld"),
  526.             AFX_IDP_PARSE_INT_RANGE);
  527. }
  528.  
  529. void AFXAPI DDV_MinMaxUInt(CDataExchange* pDX, UINT value, UINT minVal, UINT maxVal)
  530. {
  531.     ASSERT(minVal <= maxVal);
  532.     if (value < minVal || value > maxVal)
  533.         FailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%lu"),
  534.             AFX_IDP_PARSE_INT_RANGE);
  535. }
  536.  
  537. void AFXAPI DDV_MinMaxDWord(CDataExchange* pDX, DWORD value, DWORD minVal, DWORD maxVal)
  538. {
  539.     ASSERT(minVal <= maxVal);
  540.     if (value < minVal || value > maxVal)
  541.         FailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%lu"),
  542.             AFX_IDP_PARSE_INT_RANGE);
  543. }
  544.  
  545. /////////////////////////////////////////////////////////////////////////////
  546. // Max Chars Dialog Data Validation
  547.  
  548. void AFXAPI DDV_MaxChars(CDataExchange* pDX, CString const& value, int nChars)
  549. {
  550.     ASSERT(nChars >= 1);        // allow them something
  551.     if (pDX->m_bSaveAndValidate && value.GetLength() > nChars)
  552.     {
  553.         TCHAR szT[32];
  554.         wsprintf(szT, _T("%d"), nChars);
  555.         CString prompt;
  556.         AfxFormatString1(prompt, AFX_IDP_PARSE_STRING_SIZE, szT);
  557.         AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_STRING_SIZE);
  558.         prompt.Empty(); // exception prep
  559.         pDX->Fail();
  560.     }
  561.     else if (pDX->m_hWndLastControl != NULL && pDX->m_bEditLastControl)
  562.     {
  563.         // limit the control max-chars automatically
  564.         ::SendMessage(pDX->m_hWndLastControl, EM_LIMITTEXT, nChars, 0);
  565.     }
  566. }
  567.  
  568. /////////////////////////////////////////////////////////////////////////////
  569. // Special DDX_ proc for subclassing controls
  570.  
  571. void AFXAPI DDX_Control(CDataExchange* pDX, int nIDC, CWnd& rControl)
  572. {
  573.     if (rControl.m_hWnd == NULL)    // not subclassed yet
  574.     {
  575.         ASSERT(!pDX->m_bSaveAndValidate);
  576.  
  577.         HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
  578.  
  579.         if (!rControl.SubclassWindow(hWndCtrl))
  580.         {
  581.             ASSERT(FALSE);      // possibly trying to subclass twice?
  582.             AfxThrowNotSupportedException();
  583.         }
  584. #ifndef _AFX_NO_OCC_SUPPORT
  585.         else
  586.         {
  587.             // If the control has reparented itself (e.g., invisible control),
  588.             // make sure that the CWnd gets properly wired to its control site.
  589.             if (pDX->m_pDlgWnd->m_hWnd != ::GetParent(rControl.m_hWnd))
  590.                 rControl.AttachControlSite(pDX->m_pDlgWnd);
  591.         }
  592. #endif //!_AFX_NO_OCC_SUPPORT
  593.  
  594.     }
  595. }
  596.  
  597. /////////////////////////////////////////////////////////////////////////////
  598. // Global failure dialog helpers (used by database classes)
  599.  
  600. void AFXAPI AfxFailMaxChars(CDataExchange* pDX, int nChars)
  601. {
  602.     TCHAR lpszTemp[32];
  603.     wsprintf(lpszTemp, _T("%d"), nChars);
  604.     CString prompt;
  605.     AfxFormatString1(prompt, AFX_IDP_PARSE_STRING_SIZE, lpszTemp);
  606.     AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_STRING_SIZE);
  607.     prompt.Empty(); // exception prep
  608.     pDX->Fail();
  609. }
  610.  
  611. void AFXAPI AfxFailRadio(CDataExchange* pDX)
  612. {
  613.     CString prompt;
  614.     AfxFormatStrings(prompt, AFX_IDP_PARSE_RADIO_BUTTON, NULL, 0);
  615.     AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_RADIO_BUTTON);
  616.     prompt.Empty(); // exception prep
  617.     pDX->Fail();
  618. }
  619.  
  620. /////////////////////////////////////////////////////////////////////////////
  621.