home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 7.ddi / OWLDEMOS.ZIP / VDLGAPP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  6.3 KB  |  214 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include "owl.h"
  4. #include "dialog.h"
  5. #include "edit.h"
  6. #include "string.h"    // for strcpy and strcat
  7. #include "stdlib.h"    // for atoi
  8. #include "ctype.h"     // for isdigit and isalpha
  9.  
  10.  
  11. #define CM_EMPINPUT 201
  12. #define ID_NAMEEDIT 101
  13. #define ID_SSEDIT 105
  14. #define ID_IDEDIT 107
  15.  
  16. #define MAXNAMELEN 35
  17. #define MAXSSLEN 12
  18. #define MAXIDLEN 6
  19.  
  20. struct TTransferStruct {
  21.     char NameEdit[MAXNAMELEN];
  22.     char SSEdit[MAXSSLEN];
  23.     char IDEdit[MAXIDLEN];
  24. };
  25.  
  26. /* Declare TEmployeeDlg, a TDialog descendant */
  27. class TEmployeeDlg : public TDialog {
  28. public:
  29.     char EmpName[MAXNAMELEN] ;
  30.     char EmpSSNum[MAXSSLEN] ;
  31.     char EmpID[MAXIDLEN] ;
  32.     TEmployeeDlg(PTWindowsObject AParent, LPSTR name);
  33.     virtual BOOL CanClose();
  34.  
  35. private:
  36.     void FillBuffers();
  37.     BOOL ValidName();
  38.     BOOL ValidSSNum();
  39.     BOOL ValidID();
  40. };
  41.  
  42. /* Declare TTestWindow, a TWindow descendant */
  43. class TTestWindow : public TWindow {
  44. public:
  45.     TTransferStruct TransferStruct;
  46.  
  47.     TTestWindow(PTWindowsObject AParent, LPSTR ATitle);
  48.     virtual void EmpInput(RTMessage Msg) = [CM_FIRST + CM_EMPINPUT];
  49. };
  50.  
  51. /* Declare TValidateApp, a TApplication descendant */
  52. class TValidateApp : public TApplication {
  53. public:
  54.   TValidateApp(LPSTR name, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  55.     LPSTR lpCmdLine, int nCmdShow)
  56.     : TApplication(name, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  57.     virtual void InitMainWindow();
  58. };
  59.  
  60. /*--------------------------------------------------*/
  61. /* TEmployeeDlg implementations:                      */
  62. /*--------------------------------------------------*/
  63.  
  64. TEmployeeDlg::TEmployeeDlg(PTWindowsObject AParent, LPSTR name)
  65.                  :TDialog(AParent, name)
  66. {
  67.   new TEdit(this, ID_NAMEEDIT,
  68.       sizeof(((TTestWindow *)Parent)->TransferStruct.NameEdit));
  69.   new TEdit(this, ID_SSEDIT,
  70.       sizeof(((TTestWindow *)Parent)->TransferStruct.SSEdit));
  71.   new TEdit(this, ID_IDEDIT,
  72.       sizeof(((TTestWindow *)Parent)->TransferStruct.IDEdit));
  73.   TransferBuffer = (void far*)
  74.                      &(((TTestWindow *)Parent)->TransferStruct);
  75. }
  76.  
  77. /*--------------------------------------------------*/
  78. /* When OK button is pressed and CanClose called,   */
  79. /* respond by validating the employee data which    */
  80. /* was input.  The dialog is not ended until valid  */
  81. /* entries have been made in all fields.            */
  82. /*--------------------------------------------------*/
  83. BOOL TEmployeeDlg::CanClose()
  84. {
  85.   FillBuffers();
  86.  
  87.   /* Make sure all input fields have been filled in.*/
  88.   if ((EmpName[0]) && (EmpSSNum[0]) && (EmpID[0]))
  89.   {
  90.     /* Make sure all input fields have valid data */
  91.     if ( ValidName() && ValidSSNum() && ValidID() )
  92.       return TRUE;
  93.   }
  94.   else
  95.     MessageBox(HWindow,
  96.            "All fields must be filled in","Input Error", MB_OK);
  97.   return FALSE;
  98. }
  99.  
  100. /*--------------------------------------------------*/
  101. /* Retrieve data.                                   */
  102. /*--------------------------------------------------*/
  103. void TEmployeeDlg::FillBuffers()
  104. {
  105.    GetDlgItemText(HWindow, ID_NAMEEDIT, EmpName, MAXNAMELEN);
  106.    GetDlgItemText(HWindow, ID_SSEDIT, EmpSSNum, MAXSSLEN);
  107.    GetDlgItemText(HWindow, ID_IDEDIT, EmpID, MAXIDLEN);
  108. }
  109.  
  110. /*--------------------------------------------------*/
  111. /* Validate employee name.                          */
  112. /*--------------------------------------------------*/
  113. BOOL TEmployeeDlg::ValidName()
  114. {
  115.   for (int i=0; i < strlen(EmpName); i++)
  116.     if ( !isalpha(EmpName[i]) && EmpName[i] != ' ' && EmpName[i] != '.' )
  117.     {
  118.       MessageBox(HWindow,
  119.                  "Employee Name Entry is incorrect","Input Error", MB_OK);
  120.       return FALSE;
  121.     }
  122.   return TRUE;
  123. }
  124.  
  125. /*--------------------------------------------------*/
  126. /* Validate employee social security number.        */
  127. /*--------------------------------------------------*/
  128. BOOL TEmployeeDlg::ValidSSNum()
  129. {
  130.   int i , Len;
  131.   BOOL Valid;
  132.  
  133.   Valid = TRUE;
  134.   Len = strlen(EmpSSNum);
  135.   if ( (Len != 11) || (EmpSSNum[3] != '-') || (EmpSSNum[6] != '-') )
  136.      Valid = FALSE;
  137.   else
  138.      for (i=0; i < Len; i++)
  139.      {
  140.         if ( (i != 3) && (i != 6) && !(isdigit(EmpSSNum[i]) ) )
  141.         {
  142.           Valid = FALSE;
  143.           break;
  144.         }
  145.      }
  146.   if ( !Valid )
  147.      MessageBox(HWindow, "Employee Social Security number is incorrect",
  148.                 "Input Error", MB_OK);
  149.   return Valid;
  150. }
  151.  
  152. /*--------------------------------------------------*/
  153. /* Validate employee ID, must be between 1 and 10000*/
  154. /*--------------------------------------------------*/
  155. BOOL TEmployeeDlg::ValidID()
  156. {
  157.   long IDValue;
  158.  
  159.   IDValue = atoi(EmpID) ;    /* returns 0 if can't be converted */
  160.   if ( IDValue < 1 || IDValue > 10000)
  161.   {
  162.     MessageBox(HWindow, "Employee ID number is incorrect","Input Error", MB_OK);
  163.     return FALSE;
  164.   }
  165.   return TRUE;
  166. }
  167.  
  168. /*--------------------------------------------------*/
  169. /* TTestWindow implementations:                      */
  170. /*--------------------------------------------------*/
  171.  
  172. TTestWindow::TTestWindow(PTWindowsObject Parent,LPSTR ATitle)
  173.                          : TWindow(Parent, ATitle)
  174. {
  175.   AssignMenu(200);
  176.   memset(&TransferStruct, 0x0, sizeof TransferStruct);
  177. }
  178.  
  179. void TTestWindow::EmpInput(RTMessage)
  180. {
  181.   char EmpInfo[MAXNAMELEN + MAXSSLEN + MAXIDLEN + 3] ;
  182.  
  183.   if ( GetModule()->ExecDialog(
  184.                new TEmployeeDlg(this,"EMPLOYEEINFO")) == IDOK )
  185.   {
  186.     strcpy(EmpInfo, TransferStruct.NameEdit);    
  187.     strcat(EmpInfo, "\n");
  188.     strcat(EmpInfo, TransferStruct.SSEdit);    
  189.     strcat(EmpInfo, "\n");
  190.     strcat(EmpInfo, TransferStruct.IDEdit);    
  191.     MessageBox(HWindow, EmpInfo,"information stored", MB_OK);
  192.   }
  193. }
  194.  
  195. /*--------------------------------------------------*/
  196. /* TValidateApp method implementations:             */
  197. /*--------------------------------------------------*/
  198.  
  199. /* Construct the TValidateApp's MainWindow of type TTestWindow */
  200. void TValidateApp::InitMainWindow()
  201. {
  202.   MainWindow = new TTestWindow(NULL, "Validate Dialog Input");
  203. }
  204.  
  205. // Main program
  206. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  207.   LPSTR lpCmdLine, int nCmdShow)
  208. {
  209.   TValidateApp ValidateApp ("ValidateApp", hInstance, hPrevInstance,
  210.     lpCmdLine, nCmdShow);
  211.   ValidateApp.Run();
  212.   return ValidateApp.Status;
  213. }
  214.