home *** CD-ROM | disk | FTP | other *** search
/ Windoware / WINDOWARE_1_6.iso / source / wedl100 / demo1.c < prev    next >
C/C++ Source or Header  |  1991-10-14  |  12KB  |  351 lines

  1.  
  2. /*---------------------------------------------------------------------------*/
  3. /*                                                                           */
  4. /*          WEDL - Windows Enhanced Dialog Library                           */
  5. /*          Copyright (c) 1991, Mike Smedley                                 */
  6. /*          All Rights Reserved                                              */
  7. /*          Module:  DEMO1.C                                                 */
  8. /*                                                                           */
  9. /*---------------------------------------------------------------------------*/
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include "wedl.h"
  15. #include "demo.h"
  16. #include "demohelp.h"
  17.  
  18. /*---------------------------------------------------------------------------*/
  19.  
  20. static void update_date_text( HFORM hform );
  21.  
  22. /*---------------------------------------------------------------------------*/
  23.  
  24. struct states_t {
  25.     PSTR state_code;
  26.     int zip_low;
  27.     int zip_high;
  28. };
  29.  
  30. static struct states_t states[] = {
  31.     { "AK", 995, 999 },
  32.     { "AL", 350, 369 },
  33.     { "AR", 716, 729 },
  34.     { "AZ", 850, 865 },
  35.     { "CA", 900, 961 },
  36.     { "CO", 800, 816 },
  37.     { "CT",  60,  69 },
  38.     { "DE", 197, 199 },
  39.     { "FL", 320, 339 },
  40.     { "GA", 300, 319 },
  41.     { "HI", 967, 968 },
  42.     { "IA", 500, 528 },
  43.     { "ID", 832, 838 },
  44.     { "IL", 600, 629 },
  45.     { "IN", 460, 479 },
  46.     { "KS", 660, 679 },
  47.     { "KY", 400, 427 },
  48.     { "LA", 700, 714 },
  49.     { "MA",  10,  27 },
  50.     { "MD", 206, 219 },
  51.     { "ME",  39,  49 },
  52.     { "MI", 480, 499 },
  53.     { "MN", 550, 567 },
  54.     { "MO", 630, 658 },
  55.     { "MS", 386, 397 },
  56.     { "MT", 590, 599 },
  57.     { "NC", 270, 289 },
  58.     { "ND", 580, 588 },
  59.     { "NE", 680, 693 },
  60.     { "NH",  30,  38 },
  61.     { "NJ",  70,  89 },
  62.     { "NM", 870, 884 },
  63.     { "NV", 890, 898 },
  64.     { "NY", 100, 149 },
  65.     { "OH", 430, 458 },
  66.     { "OK", 730, 749 },
  67.     { "OR", 970, 979 },
  68.     { "PA", 150, 196 },
  69.     { "RI",  28,  29 },
  70.     { "SC", 290, 299 },
  71.     { "SD", 570, 577 },
  72.     { "TN", 370, 385 },
  73.     { "TX", 750, 799 },
  74.     { "UT", 840, 847 },
  75.     { "VA", 220, 246 },
  76.     { "VT",  50,  59 },
  77.     { "WA", 980, 994 },
  78.     { "WI", 530, 549 },
  79.     { "WV", 247, 268 },
  80.     { "WY", 820, 831 },
  81.     { "DC", 200, 205 },
  82.     { "GU",   0, 999 },
  83.     { "PR",   0, 999 },
  84.     { "VI",   0, 999 },
  85.     { NULL,   0,   0 }
  86. };
  87.  
  88. static PSTR months[] = {
  89.     NULL, "January", "February", "March", "April", "May", "June", "July",
  90.     "August", "September", "October", "November", "December"
  91. };
  92.  
  93. /*---------------------------------------------------------------------------*/
  94.  
  95. int FAR PASCAL DialogProc1( HWND hDlg, unsigned message, WORD wParam,
  96.                             LONG lParam )
  97. {
  98.     static HFORM hform = NULL;
  99.     static char first_name[16]="", mid_init[2]="", last_name[21]="";
  100.     static char address[31]="", city[16]="", state[3]="", zip_code[10]="";
  101.     static char soc_sec_no[10]="", phone_num[11]="", hire_date[9]="  /  /  ";
  102.  
  103.     switch( message ) {
  104.         case WM_INITDIALOG:
  105.             hform = form_begin( hDlg );
  106.             form_set_help( hform, "demohelp.hlp" );
  107.             field_define( hform, ID_SSN, soc_sec_no, DT_STRING,
  108.                           "<0..7>99'-'99'-'9(4)",
  109.                           FDF_NOTBLANK | FDF_UPDATE | FDF_COMPLETE,
  110.                           NULL, 0, HELPID_SSN );
  111.             field_define( hform, ID_FNAME, first_name, DT_STRING,
  112.                           "A(15)", FDF_CONDUPD | FDF_MIXED,
  113.                           NULL, 0, HELPID_FNAME );
  114.             field_define( hform, ID_MIDINIT, mid_init, DT_STRING,
  115.                           "A(1)'.'", FDF_UPDATE | FDF_UPPER,
  116.                           NULL, 0, HELPID_MIDINIT );
  117.             field_define( hform, ID_LNAME, last_name, DT_STRING,
  118.                           "<A..Z>A(19)", FDF_CONDUPD | FDF_MIXED,
  119.                           NULL, 0, HELPID_LNAME );
  120.             field_define( hform, ID_ADDRESS, address, DT_STRING,
  121.                           "?(30)", FDF_UPDATE | FDF_MIXED,
  122.                           NULL, 0, HELPID_ADDRESS );
  123.             field_define( hform, ID_CITY, city, DT_STRING,
  124.                           "?(15)", FDF_CONDUPD | FDF_MIXED,
  125.                           NULL, 0, HELPID_CITY );
  126.             field_define( hform, ID_STATE, state, DT_STRING,
  127.                           "A(2)", FDF_COMPLETE | FDF_UPDATE | FDF_UPPER,
  128.                           pcheck_state, BAD_STATE, HELPID_STATE );
  129.             field_define( hform, ID_ZIP, zip_code, DT_STRING,
  130.                           "<0..9>(5)'-'9(4)", FDF_UPDATE,
  131.                           pcheck_zip_code, BAD_ZIP, HELPID_ZIP );
  132.             field_define( hform, ID_PHONE, phone_num, DT_STRING,
  133.                           "'('999') '999'-'9999", FDF_UPDATE | FDF_COMPLETE,
  134.                           NULL, 0, HELPID_PHONE );
  135.             field_define( hform, ID_HIREDATE, hire_date, DT_STRING,
  136.                           " <01> 9 '/' <0123> 9 '/' <89> 9 ",
  137.                           FDF_UPDATE | FDF_COMPLETE | FDF_RDPHYS | FDF_WRPHYS,
  138.                           pcheck_date, BAD_DATE, HELPID_HIREDATE );
  139.             statmsg_define( hform, ID_INSERT, SM_INSERT, "Insert: On", "Insert: Off" );
  140.             form_end( hform );
  141.             return( TRUE );
  142.  
  143.         case WM_COMMAND:
  144.             dproc_enter_wm_command( hform, wParam, lParam );
  145.             switch( wParam ) {
  146.                 case BAD_DATE:
  147.                     dproc_enter_error( hform );
  148.                     MessageBox( hDlg, "Date Is Invalid", NULL, MB_OK );
  149.                     dproc_exit_error( hform, lParam );
  150.                     return( TRUE );
  151.                 case BAD_STATE:
  152.                     dproc_enter_error( hform );
  153.                     MessageBox( hDlg, "Invalid State Code", NULL, MB_OK );
  154.                     dproc_exit_error( hform, lParam );
  155.                     return( TRUE );
  156.                 case BAD_ZIP:
  157.                     dproc_enter_error( hform );
  158.                     MessageBox( hDlg, "Invalid Zip Code", NULL, MB_OK );
  159.                     dproc_exit_error( hform, lParam );
  160.                     return( TRUE );
  161.                 case ID_HIREDATE:
  162.                     if( HIWORD( lParam ) ==  EN_KILLFOCUS
  163.                      && !form_in_error_cond( hform )
  164.                      && !form_is_cancelled( hform ) ) {
  165.                         update_date_text( hform );
  166.                         return( TRUE );
  167.                     }
  168.                     break;
  169.                 case IDOK:
  170.                     dproc_enter_idok( hform );
  171.                     if( !form_in_error_cond( hform ) ) {
  172.                         if( form_validate( hform ) != NULL ) break;
  173.                         form_process( hform );
  174.                         EndDialog( hDlg, TRUE );
  175.                         form_terminate( hform );
  176.                         wsprintf( tbuf,
  177.                                   "Soc Sec No.\t= %s\n"
  178.                                   "Name\t\t= %s %s. %s\n"
  179.                                   "Address\t\t= %s\n"
  180.                                   "\t\t= %s, %s %.5s-%s\n"
  181.                                   "Phone No.\t= %s\n"
  182.                                   "Hire Date\t= %s",
  183.                                   (LPSTR) soc_sec_no,
  184.                                   (LPSTR) first_name,
  185.                                   (LPSTR) mid_init, (LPSTR) last_name,
  186.                                   (LPSTR) address, (LPSTR) city,
  187.                                   (LPSTR) state, (LPSTR) zip_code,
  188.                                   (LPSTR) zip_code+5, (LPSTR) phone_num,
  189.                                   (LPSTR) hire_date );
  190.                         MessageBox( NULL, tbuf, "Field Contents", MB_OK );
  191.                     }
  192.                     return( TRUE );
  193.                 case IDCANCEL:
  194.                     dproc_enter_idcancel( hform );
  195.                     EndDialog( hDlg, TRUE );
  196.                     form_terminate( hform );
  197.                     return( TRUE );
  198.             }
  199.             break;
  200.     }
  201.     return( FALSE );
  202. }
  203.  
  204. /*---------------------------------------------------------------------------*/
  205.  
  206. int FAR PASCAL check_date( LPSTR pbuf )
  207. {
  208.     char date[10];
  209.     int month, day, year;
  210.  
  211.     if( !str_is_blank( pbuf ) ) {
  212.  
  213.         /* parse year, day, and month from buffer */
  214.         lstrcpy( date, pbuf );
  215.         year = 1900 + atoi( date+4 );
  216.         *( date+4 ) = '\0';
  217.         day = atoi( date+2 );
  218.         *( date+2 ) = '\0';
  219.         month = atoi( date );
  220.  
  221.         /* validate month */
  222.         if( month > 12 ) return( 1 );
  223.  
  224.         /* validate day and month */
  225.         if( day < 1 ) return( 3 );
  226.         switch( month ) {
  227.             case 2:
  228.                 if( year%4 )
  229.                     if( day > 29 ) return( 3 );
  230.                 else
  231.                     if( day > 28 ) return( 3 );
  232.                 break;
  233.             case 1:
  234.             case 3:
  235.             case 5:
  236.             case 7:
  237.             case 8:
  238.             case 10:
  239.             case 12:
  240.                 if( day > 31 ) return( 3 );
  241.                 break;
  242.             case 4:
  243.             case 6:
  244.             case 9:
  245.             case 11:
  246.                 if( day > 30 ) return( 3 );
  247.                 break;
  248.             default:
  249.                 return( 1 );
  250.         }
  251.     }
  252.  
  253.     /* return zero indicating no error */
  254.     return( 0 );
  255. }
  256.  
  257. /*---------------------------------------------------------------------------*/
  258.  
  259. int FAR PASCAL check_state( LPSTR pbuf )
  260. {
  261.     int i;
  262.  
  263.     /* allow state to be blank */
  264.     if( str_is_blank( pbuf ) ) return( 0 );
  265.  
  266.     /* do for all state codes in the table */
  267.     for( i = 0 ; states[i].state_code != NULL ; i++ )
  268.  
  269.         /* if state code was found, then no error */
  270.         if( !lstrcmp( states[i].state_code, pbuf ) ) return( 0 );
  271.  
  272.     /* not a legal 2-letter state code */
  273.     return( 1 );
  274. }
  275.  
  276. /*---------------------------------------------------------------------------*/
  277.  
  278. int FAR PASCAL check_zip_code( LPSTR pbuf )
  279. {
  280.     HFORM hform;
  281.     HFIELD hfield;
  282.     LPSTR p;
  283.     int i, num_spaces;
  284.     long zip, zip_low, zip_high;
  285.     BOOL valid;
  286.  
  287.     /* allow zip code to be blank */
  288.     if( str_is_blank( pbuf ) ) return( 0 );
  289.  
  290.     /* count spaces in the extended portion of the 9-digit zip code */
  291.     num_spaces = 0;
  292.     for( p = pbuf + 5 ; *p != '\0' ; p++ ) if( *p == ' ' ) num_spaces++;
  293.  
  294.     /* if zip code isn't exactly 5 or 9 digits, then there's an error */
  295.     if( num_spaces != 0 && num_spaces != 4 ) return( 6 );
  296.  
  297.     *( pbuf+5 ) = '\0';
  298.     hform = form_get_active();
  299.     hfield = field_get_from_ctrl_id( hform, ID_ZIP );
  300.     field_log_to_data( hfield, pbuf, &zip, DT_LONG );
  301.  
  302.     /* search for matching zip code */
  303.     valid = FALSE;
  304.     for( i = 0 ; states[i].state_code != NULL ; i++ ) {
  305.         zip_low  = (long) states[i].zip_low  * 100L;
  306.         zip_high = (long) states[i].zip_high * 100L;
  307.         if( zip >= zip_low && zip <= zip_high ) {
  308.             valid = TRUE;
  309.             break;
  310.         }
  311.     }
  312.  
  313.     /* make sure state field code matches that of the input zip code */
  314.     if( valid ) {
  315.         hfield = field_get_from_ctrl_id( hform, ID_STATE );
  316.         field_get_text( hfield, tbuf, FALSE );
  317.         if( !str_is_blank( tbuf ) )
  318.             if( lstrcmp( tbuf, states[i].state_code ) ) valid = FALSE;
  319.     }
  320.  
  321.     return( valid ? 0 : 1 );
  322. }
  323.  
  324. /*---------------------------------------------------------------------------*/
  325.  
  326. static void update_date_text( HFORM hform )
  327. {
  328.     LPFORM lpform;
  329.     int day, month, year;
  330.     char text[128], buf[9], date[10];
  331.  
  332.     field_get_text( field_get_from_ctrl_id( hform, ID_HIREDATE ), date, TRUE );
  333.  
  334.     lstrcpy( buf, date );
  335.     year = 1900 + atoi( buf+6 );
  336.     *( buf+5 ) = '\0';
  337.     day = atoi( buf+3 );
  338.     *( buf+2 ) = '\0';
  339.     month = atoi( buf );
  340.  
  341.     if( month >= 1 && month <= 12 )
  342.         wsprintf( text, "(%s %d, %d)", (LPSTR) months[ month ], day, year );
  343.     else
  344.         *text = '\0';
  345.  
  346.     lpform = form_lock( hform );
  347.     SetDlgItemText( lpform->hdlg, ID_HIRETEXT, text );
  348.     form_unlock( hform );
  349. }
  350.  
  351.