home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / chkbook / checkdlg.cpp next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  7.5 KB  |  323 lines

  1. // CheckDlg.cpp : implementation file for CCheckDlg
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "chkbook.h"
  15. #include "CheckDlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CCheckDlg dialog
  25.  
  26.  
  27. CCheckDlg::CCheckDlg(CWnd* pParent /*=NULL*/)
  28.     : CDialog(CCheckDlg::IDD, pParent)
  29. {
  30.     //{{AFX_DATA_INIT(CCheckDlg)
  31.     m_strMemo = _T("");
  32.     m_strPayTo = _T("");
  33.     m_strCheckNo = _T("");
  34.     m_strDate = _T("");
  35.     m_dwCents = 0;
  36.     m_strAmount = _T("");
  37.     //}}AFX_DATA_INIT
  38. }
  39.  
  40.  
  41. BEGIN_MESSAGE_MAP(CCheckDlg, CDialog)
  42.     //{{AFX_MSG_MAP(CCheckDlg)
  43.         // NOTE: the ClassWizard will add message map macros here
  44.     //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CCheckDlg message handlers
  49.  
  50. BOOL GetDollarsCents(HWND hWnd, DWORD& dwCents)
  51. {
  52.     TCHAR szWindowText[20];
  53.     ::GetWindowText(hWnd, szWindowText, 19);
  54.     DWORD dwDollars;
  55.     int nCents;
  56.     TCHAR* psz;
  57.     TCHAR* pszDollars;
  58.     TCHAR* pszCents;
  59.  
  60.     // strip leading blanks
  61.     for (pszDollars = szWindowText;  *pszDollars == ' ';  pszDollars++)
  62.     {
  63.         if (*pszDollars == 0)
  64.         {
  65.             dwCents = 0;
  66.             return TRUE;
  67.         }
  68.     }
  69.  
  70.     // parse dollar amount, before optional decimal point
  71.     for (psz = pszDollars; (*psz != '.') && (*psz != ' ') && (*psz != 0); psz++)
  72.     {
  73.         if ((*psz < '0') || (*psz > '9'))
  74.             return FALSE;
  75.     }
  76.     BOOL bDollarsOnly = (*psz == 0);
  77.     *psz = 0;
  78.  
  79.     if (_tcslen(pszDollars) > 8)
  80.         return FALSE;
  81.     if (_tcslen(pszDollars) == 0)
  82.     {
  83.         dwDollars = 0L;
  84.     }
  85.     else
  86.     {
  87.         dwDollars = _ttol(pszDollars);
  88.         if (dwDollars > ((DWORD)0xffffffff)/100)
  89.             return FALSE;
  90.     }
  91.  
  92.     if (bDollarsOnly)
  93.     {
  94.         nCents = 0;
  95.     }
  96.     else  // decimal point was found
  97.     {
  98.         // parse cents
  99.         for (pszCents = ++psz; (*psz != 0) && (*psz != ' '); psz++)
  100.         {
  101.             if ((*psz < '0') || (*psz > '9'))
  102.                 return FALSE;
  103.         }
  104.         if (*psz == ' ')
  105.         {
  106.             for (psz++ ; *psz != 0; psz++)
  107.             {
  108.                 if (*psz != ' ')
  109.                     return FALSE;
  110.             }
  111.         }
  112.  
  113.         int nCentsStrLen = _tcslen(pszCents);
  114.         switch (nCentsStrLen)
  115.         {
  116.             case 0:
  117.                 nCents = 0;
  118.                 break;
  119.             case 1:
  120.                 nCents = _ttoi(pszCents) * 10;
  121.                 break;
  122.             case 2:
  123.                 nCents = _ttoi(pszCents);
  124.                 break;
  125.             default:
  126.                 return FALSE;
  127.         }
  128.     }
  129.  
  130.     dwCents = dwDollars * 100 + nCents;
  131.     return TRUE;
  132. }
  133.  
  134. CString GetDollarsCentsFormatted(DWORD dwCents)
  135. {
  136.     DWORD dwDollars = dwCents / 100;
  137.     WORD wCents = (WORD)(dwCents - 100 * dwDollars);
  138.  
  139.     CString str;
  140.     str.Format(_T("%lu.%02u"), dwDollars, wCents);
  141.     return str;
  142. }
  143.  
  144. void SetDollarsCents(HWND hWnd, DWORD dwCents)
  145. {
  146.     // Convert the DWORD dollars/cents value to a string and
  147.     // display it in the dollars/cents control.
  148.  
  149.     // If the dollar cent field has been previously determined by
  150.     // DDX_DollarsCents() to be invalid, then don't update it.
  151.     // Leave the invalid data in the field so the user can correct
  152.     // it, rather than replace it with the literal translation
  153.     // of the INVALID_DOLLARS_CENTS #define value.
  154.  
  155.     CString str = GetDollarsCentsFormatted(dwCents);
  156.     ::SetWindowText(hWnd, str.GetBufferSetLength(20));
  157. }
  158.  
  159. void AFXAPI DDX_DollarsCents(CDataExchange* pDX, int nIDC, DWORD& dwCents)
  160. {
  161.     HWND hWndCtrl = pDX->PrepareEditCtrl(nIDC);
  162.     if (pDX->m_bSaveAndValidate)
  163.     {
  164.         if (!GetDollarsCents(hWndCtrl, dwCents))
  165.         {
  166.             AfxMessageBox(CString((LPCTSTR)IDS_MESSAGE1));
  167.             pDX->Fail();
  168.         }
  169.     }
  170.     else
  171.     {
  172.         SetDollarsCents(hWndCtrl, dwCents);
  173.     }
  174. }
  175.  
  176. void CCheckDlg::DoDataExchange(CDataExchange* pDX)
  177. {
  178.     CDialog::DoDataExchange(pDX);
  179.     //{{AFX_DATA_MAP(CCheckDlg)
  180.     DDX_Text(pDX, IDC_MEMO, m_strMemo);
  181.     DDV_MaxChars(pDX, m_strMemo, 39);
  182.     DDX_Text(pDX, IDC_PAYTO, m_strPayTo);
  183.     DDV_MaxChars(pDX, m_strPayTo, 39);
  184.     DDX_Text(pDX, IDC_CHECKNO, m_strCheckNo);
  185.     DDV_MaxChars(pDX, m_strCheckNo, 10);
  186.     DDX_Text(pDX, IDC_DATE, m_strDate);
  187.     DDV_MaxChars(pDX, m_strDate, 9);
  188.     DDX_DollarsCents(pDX, IDC_AMOUNTNUM, m_dwCents);
  189.     DDX_Text(pDX, IDC_AMOUNTTEXT, m_strAmount);
  190.     //}}AFX_DATA_MAP
  191. }
  192.  
  193. CString CCheckDlg::GetDollarsCentsText(DWORD dwCents)
  194. {
  195.     CString str, strTemp;
  196.     DWORD dwDollars = dwCents / 100;
  197.     WORD wCents = (WORD)(dwCents - (dwDollars * 100));
  198.     if (dwDollars == 0L)
  199.     {
  200.         str.LoadString(IDS_ONES_0);   // "Zero"
  201.         str += ' ';
  202.     }
  203.     else
  204.     {
  205.         if (dwDollars >= 1000000)
  206.         {
  207.             DWORD dwMillions = dwDollars / 1000000;
  208.             CString strMillions = HundredsTensOnes(dwMillions);
  209.             strTemp.LoadString(IDS_MILLION);    // "Million"
  210.             str = strMillions;
  211.             str += ' ';
  212.             str += strTemp;
  213.             str += ' ';
  214.             dwDollars -= (dwMillions * 1000000);
  215.         }
  216.         if (dwDollars >= 1000)
  217.         {
  218.             DWORD dwThousands = dwDollars / 1000;
  219.             CString strThousands = HundredsTensOnes(dwThousands);
  220.             strTemp.LoadString(IDS_THOUSAND);   // "Thousand"
  221.             str += strThousands;
  222.             str += ' ';
  223.             str += strTemp;
  224.             str += ' ';
  225.             dwDollars -= (dwThousands * 1000);
  226.         }
  227.         if (dwDollars > 0)
  228.         {
  229.             CString strHundredsTensOnes = HundredsTensOnes(dwDollars);
  230.             str += strHundredsTensOnes;
  231.             str += ' ';
  232.         }
  233.     }
  234.     CString strCents;
  235.     strCents.Format(_T("%02u"), wCents);
  236.     strTemp.LoadString(IDS_AND);    // "and"
  237.     str += strTemp;
  238.     str += ' ';
  239.     str += strCents;
  240.     strTemp.LoadString(IDS_HUNDRETHS_DOLLARS);  // "/100ths Dollars"
  241.     str += strTemp;
  242.     return str;
  243. }
  244.  
  245. /////////////////////////////////////////////////////////////////////////////
  246. // Implementation
  247.  
  248. CString CCheckDlg::HundredsTensOnes(DWORD dwHundredsTensOnes)
  249. {
  250.     CString str, strTemp;
  251.     if (dwHundredsTensOnes >= 100)
  252.     {
  253.         DWORD dwHundreds = dwHundredsTensOnes / 100;
  254.         CString strHundreds;
  255.         strHundreds.LoadString(IDS_ONES_0 + dwHundreds);
  256.         strTemp.LoadString(IDS_HUNDRED);
  257.         str = strHundreds;
  258.         str += ' ';
  259.         str += strTemp;
  260.         str += ' ';
  261.         dwHundredsTensOnes -= (dwHundreds * 100);
  262.     }
  263.     if (dwHundredsTensOnes > 0)
  264.     {
  265.         CString strTensOnes = TensOnes(dwHundredsTensOnes);
  266.         str += strTensOnes;
  267.     }
  268.     return str;
  269. }
  270.  
  271.  
  272. CString CCheckDlg::TensOnes(DWORD dwTensOnes)
  273. {
  274.     CString str, strTemp;
  275.     if (dwTensOnes > 19)
  276.     {
  277.         DWORD dwTens = dwTensOnes / 10;
  278.         strTemp.LoadString(IDS_TENS_0 + dwTens);
  279.         str += strTemp;
  280.         dwTensOnes -= (dwTens * 10);
  281.         if (dwTensOnes > 0)
  282.         {
  283.             CString strOnes;
  284.             strOnes.LoadString(IDS_ONES_0 + dwTensOnes);
  285.             str += '-';
  286.             str += strOnes;
  287.         }
  288.     }
  289.     else
  290.     if (dwTensOnes >= 10)
  291.     {
  292.         CString strTeens;
  293.         strTeens.LoadString(IDS_TEENS_10 + dwTensOnes - 10);
  294.         str += strTeens;
  295.     }
  296.     else
  297.     {
  298.         CString strOnes;
  299.         strOnes.LoadString(IDS_ONES_0 + dwTensOnes);
  300.         str += strOnes;
  301.     }
  302.     return str;
  303. }
  304.  
  305. LRESULT CCheckDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
  306. {
  307.     // The dialog box is either laid-out for an H/PC 2.0 or a Palm-Size PC. 
  308.     // Now size-to-fit (move lower right corner so dialog box is same size as
  309.     // the frame window). 
  310.     if(message == WM_INITDIALOG)
  311.     {
  312.         CRect rect;
  313.         GetParent()->GetClientRect(&rect);
  314.         MoveWindow(&rect);
  315.     }
  316.  
  317.     // Do not wish to close dialog until it is destroyed
  318.     if ((message == WM_COMMAND) && (wParam <= 2))
  319.         return 1;
  320.  
  321.     return CDialog::WindowProc(message, wParam, lParam);
  322. }
  323.