home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / cluster / smbsmp / smbsmpex / ddxddv.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-03  |  7.2 KB  |  290 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Copyright (c) 1997 <company name>
  4. //
  5. //    Module Name:
  6. //        DDxDDv.cpp
  7. //
  8. //    Abstract:
  9. //        Implementation of custom dialog data exchange/dialog data validation
  10. //        routines.
  11. //
  12. //    Author:
  13. //        <name> (<e-mail name>) Mmmm DD, 1997
  14. //
  15. //    Revision History:
  16. //
  17. //    Notes:
  18. //        The IDS_REQUIRED_FIELD_EMPTY string resource must be defined in
  19. //        the resource file.
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22.  
  23. #include "stdafx.h"
  24. #include "DDxDDv.h"
  25.  
  26. #include "resource.h"
  27.  
  28. #ifdef _DEBUG
  29. #define new DEBUG_NEW
  30. #undef THIS_FILE
  31. static char THIS_FILE[] = __FILE__;
  32. #endif
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // Static Function Prototypes
  36. /////////////////////////////////////////////////////////////////////////////
  37. static void CleanupLabel(LPTSTR psz);
  38.  
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. //++
  42. //
  43. //    DDX_Number
  44. //
  45. //    Routine Description:
  46. //        Do data exchange between the dialog and the class.
  47. //
  48. //    Arguments:
  49. //        pDX            [IN OUT] Data exchange object 
  50. //        nIDC        [IN] Control ID.
  51. //        dwValue        [IN OUT] Value to set or get.
  52. //        dwMin        [IN] Minimum value.
  53. //        dwMax        [IN] Maximum value.
  54. //        bSigned        [IN] TRUE = value is signed, FALSE = value is unsigned
  55. //
  56. //    Return Value:
  57. //        None.
  58. //
  59. //--
  60. /////////////////////////////////////////////////////////////////////////////
  61. void AFXAPI DDX_Number(
  62.     IN OUT CDataExchange *    pDX,
  63.     IN int                    nIDC,
  64.     IN OUT DWORD &            rdwValue,
  65.     IN DWORD                dwMin,
  66.     IN DWORD                dwMax,
  67.     IN BOOL                    bSigned
  68.     )
  69. {
  70.     HWND    hwndCtrl;
  71.     DWORD    dwValue;
  72.  
  73.     ASSERT(pDX != NULL);
  74. #ifdef _DEBUG
  75.     if (bSigned)
  76.     {
  77.         ASSERT((LONG) dwMin < (LONG) dwMax);
  78.     }
  79.     else
  80.     {
  81.         ASSERT(dwMin < dwMax);
  82.     }
  83. #endif // _DEBUG
  84.  
  85.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  86.  
  87.     // Get the control window handle.
  88.     hwndCtrl = pDX->PrepareEditCtrl(nIDC);
  89.  
  90.     if (pDX->m_bSaveAndValidate)
  91.     {
  92.         BOOL    bTranslated;
  93.  
  94.         // Get the number from the control.
  95.         dwValue = GetDlgItemInt(pDX->m_pDlgWnd->m_hWnd, nIDC, &bTranslated, bSigned);
  96.  
  97.         // If the retrival failed, it is a signed number, and the minimum
  98.         // value is the smallest negative value possible, check the string itself.
  99.         if (!bTranslated && bSigned && (dwMin == 0x80000000))
  100.         {
  101.             UINT    cch;
  102.             TCHAR    szNumber[20];
  103.  
  104.             // See if it is the smallest negative number.
  105.             cch = GetDlgItemText(pDX->m_pDlgWnd->m_hWnd, nIDC, szNumber, sizeof(szNumber) / sizeof(TCHAR));
  106.             if ((cch != 0) && (lstrcmp(szNumber, _T("-2147483648")) == 0))
  107.             {
  108.                 dwValue = 0x80000000;
  109.                 bTranslated = TRUE;
  110.             }  // if:  text retrieved successfully and is highest negative number
  111.         }  // if:  error translating number and getting signed number
  112.  
  113.         // If the retrieval failed or the specified number is
  114.         // out of range, display an error.
  115.         if (   !bTranslated
  116.             || (bSigned && (((LONG) dwValue < (LONG) dwMin) || ((LONG) dwValue > (LONG) dwMax)))
  117.             || (!bSigned && ((dwValue < dwMin) || (dwValue > dwMax)))
  118.             )
  119.         {
  120.             TCHAR szMin[32];
  121.             TCHAR szMax[32];
  122.             CString strPrompt;
  123.  
  124.             if (bSigned)
  125.             {
  126.                 wsprintf(szMin, _T("%d%"), dwMin);
  127.                 wsprintf(szMax, _T("%d%"), dwMax);
  128.             }  // if:  signed number
  129.             else
  130.             {
  131.                 wsprintf(szMin, _T("%u%"), dwMin);
  132.                 wsprintf(szMax, _T("%u%"), dwMax);
  133.             }  // else:  unsigned number
  134.             AfxFormatString2(strPrompt, AFX_IDP_PARSE_INT_RANGE, szMin, szMax);
  135.             AfxMessageBox(strPrompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_INT_RANGE);
  136.             strPrompt.Empty(); // exception prep
  137.             pDX->Fail();
  138.         }  // if:  invalid string
  139.         else
  140.             rdwValue = dwValue;
  141.     }  // if:  saving data
  142.     else
  143.     {
  144.         CString        strMinValue;
  145.         CString        strMaxValue;
  146.         UINT        cchMax;
  147.  
  148.         // Set the maximum number of characters that can be entered.
  149.         if (bSigned)
  150.         {
  151.             strMinValue.Format(_T("%d"), dwMin);
  152.             strMaxValue.Format(_T("%d"), dwMax);
  153.         }  // if:  signed value
  154.         else
  155.         {
  156.             strMinValue.Format(_T("%u"), dwMin);
  157.             strMaxValue.Format(_T("%u"), dwMax);
  158.         }  // else:  unsigned value
  159.         cchMax = max(strMinValue.GetLength(), strMaxValue.GetLength());
  160.         SendMessage(hwndCtrl, EM_LIMITTEXT, cchMax, 0);
  161.  
  162.         // Set the value into the control.
  163.         if (bSigned)
  164.         {
  165.             LONG lValue = (LONG) rdwValue;
  166.             DDX_Text(pDX, nIDC, lValue);
  167.         }  // if:  signed value
  168.         else
  169.             DDX_Text(pDX, nIDC, rdwValue);
  170.     }  // else:  setting data onto the dialog
  171.  
  172. }  //*** DDX_Number()
  173.  
  174. /////////////////////////////////////////////////////////////////////////////
  175. //++
  176. //
  177. //    DDV_RequiredText
  178. //
  179. //    Routine Description:
  180. //        Validate that the dialog string is present.
  181. //
  182. //    Arguments:
  183. //        pDX            [IN OUT] Data exchange object 
  184. //        nIDC        [IN] Control ID.
  185. //        nIDCLabel    [IN] Label control ID.
  186. //        rstrValue    [IN] Value to set or get.
  187. //
  188. //    Return Value:
  189. //        None.
  190. //
  191. //--
  192. /////////////////////////////////////////////////////////////////////////////
  193. void AFXAPI DDV_RequiredText(
  194.     IN OUT CDataExchange *    pDX,
  195.     IN int                    nIDC,
  196.     IN int                    nIDCLabel,
  197.     IN const CString &        rstrValue
  198.     )
  199. {
  200.     ASSERT(pDX != NULL);
  201.  
  202.     AFX_MANAGE_STATE(AfxGetStaticModuleState());
  203.  
  204.     if (pDX->m_bSaveAndValidate)
  205.     {
  206.         if (rstrValue.GetLength() == 0)
  207.         {
  208.             HWND        hwndLabel;
  209.             TCHAR        szLabel[1024];
  210.             CString        strPrompt;
  211.  
  212.             // Get the label window handle
  213.             hwndLabel = pDX->PrepareEditCtrl(nIDCLabel);
  214.  
  215.             // Get the text of the label.
  216.             GetWindowText(hwndLabel, szLabel, sizeof(szLabel) / sizeof(TCHAR));
  217.  
  218.             // Remove ampersands (&) and colons (:).
  219.             CleanupLabel(szLabel);
  220.  
  221.             // Format and display a message.
  222.             strPrompt.FormatMessage(IDS_REQUIRED_FIELD_EMPTY, szLabel);
  223.             AfxMessageBox(strPrompt, MB_ICONEXCLAMATION);
  224.  
  225.             // Do this so that the control receives focus.
  226.             (void) pDX->PrepareEditCtrl(nIDC);
  227.  
  228.             // Fail the call.
  229.             strPrompt.Empty();    // exception prep
  230.             pDX->Fail();
  231.         }  // if:  field not specified
  232.     }  // if:  saving data
  233.  
  234. }  //*** DDV_RequiredText()
  235.  
  236. /////////////////////////////////////////////////////////////////////////////
  237. //++
  238. //
  239. //    CleanupLabel
  240. //
  241. //    Routine Description:
  242. //        Prepare a label read from a dialog to be used as a string in a
  243. //        message by removing ampersands (&) and colons (:).
  244. //
  245. //    Arguments:
  246. //        pszLabel    [IN OUT] Label to be cleaned up.
  247. //
  248. //    Return Value:
  249. //        None.
  250. //
  251. //--
  252. /////////////////////////////////////////////////////////////////////////////
  253. static void CleanupLabel(LPTSTR pszLabel)
  254. {
  255.     LPTSTR    pIn, pOut;
  256.     LANGID    langid;
  257.     WORD    primarylangid;
  258.     BOOL    bFELanguage;
  259.  
  260.     // Get the language ID.
  261.     langid = GetUserDefaultLangID();
  262.     primarylangid = (WORD) PRIMARYLANGID(langid);
  263.     bFELanguage = ((primarylangid == LANG_JAPANESE)
  264.                     || (primarylangid == LANG_CHINESE)
  265.                     || (primarylangid == LANG_KOREAN));
  266.  
  267.     //
  268.     // copy the name sans '&' and ':' chars
  269.     //
  270.  
  271.     pIn = pOut = pszLabel;
  272.     do
  273.     {
  274.         //
  275.         // strip FE accelerators with parentheses. e.g. "foo(&F)" -> "foo"
  276.         //
  277.         if (   bFELanguage
  278.             && (pIn[0] == _T('('))
  279.             && (pIn[1] == _T('&'))
  280.             && (pIn[2] != _T('\0'))
  281.             && (pIn[3] == _T(')')))
  282.         {
  283.             pIn += 3;
  284.         }
  285.         else if ((*pIn != _T('&')) && (*pIn != _T(':')))
  286.             *pOut++ = *pIn;
  287.     } while (*pIn++ != _T('\0')) ;
  288.  
  289. }  //*** CleanupLabel()
  290.