home *** CD-ROM | disk | FTP | other *** search
/ CICA 1992 November / CICA_MS_Windows_CD-ROM_Walnut_Creek_November_1992.iso / win3 / programr / wedl20 / demo.c < prev    next >
C/C++ Source or Header  |  1992-07-22  |  38KB  |  922 lines

  1.  
  2. /*---------------------------------------------------------------------------*/
  3. /*                                                                           */
  4. /*          WEDL(tm) - Windows Enhanced Dialog Library                       */
  5. /*          Copyright (c) 1991-1992, Nemisoft, Inc.                          */
  6. /*          All Rights Reserved                                              */
  7. /*          Module:  DEMO.C                                                  */
  8. /*                                                                           */
  9. /*---------------------------------------------------------------------------*/
  10.  
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "wedl.h"
  16. #include "demo.h"
  17. #include "demohelp.h"
  18.  
  19. /*---------------------------------------------------------------------------*/
  20.  
  21. /* general function prototypes */
  22.  
  23. int FAR PASCAL AboutDlgProc( HWND hDlg, unsigned message, WORD wParam,
  24.                              LONG lParam );
  25. int FAR PASCAL AlbumDlgProc( HWND hDlg, unsigned message, WORD wParam,
  26.                              LONG lParam );
  27. int FAR PASCAL EmployeeDlgProc( HWND hDlg, unsigned message, WORD wParam,
  28.                                 LONG lParam );
  29. int FAR PASCAL check_date( HFORM hform, HFIELD hfield, LPSTR pbuf );
  30. int FAR PASCAL check_file_name( HFORM hform, HFIELD hfield, LPSTR pbuf );
  31. int FAR PASCAL check_state( HFORM hform, HFIELD hfield, LPSTR pbuf );
  32. int FAR PASCAL check_zip_code( HFORM hform, HFIELD hfield, LPSTR pbuf );
  33. BOOL FAR PASCAL error_func( HFORM hform, HFIELD hfield, int error_value,
  34.                             int error_position, int error_event );
  35. static BOOL initialize_application( HANDLE hInstance );
  36. static BOOL initialize_instance( HANDLE hInstance, int nCmdShow );
  37. long FAR PASCAL MainWndProc( HWND hWnd, unsigned message, WORD wParam,
  38.                              LONG lParam );
  39. int FAR PASCAL NumericDlgProc( HWND hDlg, unsigned message, WORD wParam,
  40.                                LONG lParam );
  41. int FAR PASCAL UploadDlgProc( HWND hDlg, unsigned message, WORD wParam,
  42.                              LONG lParam );
  43.  
  44. /*---------------------------------------------------------------------------*/
  45.  
  46. /* static variables (global to this file) */
  47.  
  48. static HINSTANCE hInst;
  49. static HBITMAP hBitmap;
  50. static BITMAP bitmap;
  51. static char szClassName[] = "WEDL_DEMO";
  52. static char tbuf[512];
  53. static PVALFUNC pcheck_date, pcheck_state, pcheck_zip_code, pcheck_file_name;
  54. static PERRFUNC perror_func;
  55.  
  56. /*---------------------------------------------------------------------------*/
  57.  
  58. int PASCAL WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
  59.                     int nCmdShow )
  60. {
  61.     MSG msg;
  62.  
  63.     if( hPrevInstance == NULL )
  64.         if( !initialize_application( hInstance ) ) return( FALSE );
  65.  
  66.     if( !initialize_instance( hInstance, nCmdShow ) ) return( FALSE );
  67.  
  68.     while( GetMessage( &msg, NULL, 0, 0 ) ) {
  69.         TranslateMessage( &msg );
  70.         DispatchMessage( &msg );
  71.     }
  72.  
  73.     DeleteObject( hBitmap );
  74.  
  75.     return( msg.wParam );
  76. }
  77.  
  78. /*---------------------------------------------------------------------------*/
  79.  
  80. long FAR PASCAL MainWndProc( HWND hWnd, unsigned message, WORD wParam,
  81.                              LONG lParam )
  82. {
  83.     FARPROC pdialog_proc;
  84.     HDC hDC, hMemDC;
  85.     PAINTSTRUCT paint;
  86.  
  87.     switch( message ) {
  88.  
  89.         case WM_PAINT:
  90.             hDC     = BeginPaint( hWnd, &paint );
  91.             hMemDC  = CreateCompatibleDC( hDC );
  92.             SelectObject( hMemDC, hBitmap );
  93.             BitBlt( hDC, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hMemDC,
  94.                     0, 0, SRCCOPY );
  95.             DeleteDC( hMemDC );
  96.             EndPaint( hWnd, &paint );
  97.             break;
  98.  
  99.         case WM_COMMAND:
  100.             switch( wParam ) {
  101.                 case IDM_EMPLOYEE:
  102.                     pdialog_proc    = MakeProcInstance( EmployeeDlgProc, hInst );
  103.                     pcheck_date     = (PVALFUNC) MakeProcInstance( check_date, hInst );
  104.                     pcheck_state    = (PVALFUNC) MakeProcInstance( check_state, hInst );
  105.                     pcheck_zip_code = (PVALFUNC) MakeProcInstance( check_zip_code, hInst );
  106.                     perror_func     = (PERRFUNC) MakeProcInstance( error_func, hInst );
  107.                     DialogBox( hInst, "EmployeeInfo", hWnd, pdialog_proc );
  108.                     FreeProcInstance( perror_func );
  109.                     FreeProcInstance( pcheck_zip_code );
  110.                     FreeProcInstance( pcheck_state );
  111.                     FreeProcInstance( pcheck_date );
  112.                     FreeProcInstance( pdialog_proc );
  113.                     break;
  114.                 case IDM_ALBUM:
  115.                     pdialog_proc = MakeProcInstance( AlbumDlgProc, hInst );
  116.                     DialogBox( hInst, "AlbumDatabase", hWnd, pdialog_proc );
  117.                     FreeProcInstance( pdialog_proc );
  118.                     break;
  119.                 case IDM_UPLOAD:
  120.                     pdialog_proc     = MakeProcInstance( UploadDlgProc,   hInst );
  121.                     pcheck_file_name = (PVALFUNC) MakeProcInstance( check_file_name, hInst );
  122.                     perror_func      = (PERRFUNC) MakeProcInstance( error_func,      hInst );
  123.                     DialogBox( hInst, "UploadFile", hWnd, pdialog_proc );
  124.                     FreeProcInstance( perror_func );
  125.                     FreeProcInstance( pcheck_file_name );
  126.                     FreeProcInstance( pdialog_proc );
  127.                     break;
  128.                 case IDM_NUMERIC:
  129.                     pdialog_proc = MakeProcInstance( NumericDlgProc, hInst );
  130.                     DialogBox( hInst, "NumericFields", hWnd, pdialog_proc );
  131.                     FreeProcInstance( pdialog_proc );
  132.                     break;
  133.                 case IDM_EXIT:
  134.                     SendMessage( hWnd, WM_CLOSE, 0, 0 );
  135.                     break;
  136.                 case IDM_ABOUT:
  137.                     pdialog_proc = MakeProcInstance( AboutDlgProc, hInst );
  138.                     DialogBox( hInst, "AboutWEDL", hWnd, pdialog_proc );
  139.                     FreeProcInstance( pdialog_proc );
  140.                     break;
  141.             }
  142.             break;
  143.         case WM_DESTROY:
  144.             PostQuitMessage( 0 );
  145.             break;
  146.         default:
  147.             return( DefWindowProc( hWnd, message, wParam, lParam ) );
  148.     }
  149.     return( 0 );
  150. }
  151.  
  152. /*---------------------------------------------------------------------------*/
  153.  
  154. int FAR PASCAL AboutDlgProc( HWND hDlg, unsigned message, WORD wParam,
  155.                              LONG lParam )
  156. {
  157.     switch( message ) {
  158.         case WM_INITDIALOG:
  159.             return( TRUE );
  160.         case WM_COMMAND:
  161.             if( wParam == IDOK || wParam == IDCANCEL ) {
  162.                 EndDialog( hDlg, TRUE );
  163.                 return( TRUE );
  164.             }
  165.             break;
  166.     }
  167.     return( FALSE );
  168. }
  169.  
  170. /*---------------------------------------------------------------------------*/
  171.  
  172. static BOOL initialize_application( HANDLE hInstance )
  173. {
  174.     WNDCLASS wc;
  175.  
  176.     wc.style         = NULL;
  177.     wc.lpfnWndProc   = (WNDPROC) MainWndProc;
  178.     wc.cbClsExtra    = 0;
  179.     wc.cbWndExtra    = 0;
  180.     wc.hInstance     = hInstance;
  181.     wc.hIcon         = LoadIcon( hInstance, "WEDL" );
  182.     wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
  183.     wc.hbrBackground = GetStockObject( NULL_BRUSH );
  184.     wc.lpszMenuName  = "MainMenu";
  185.     wc.lpszClassName = szClassName;
  186.  
  187.     return( RegisterClass( &wc ) );
  188. }
  189.  
  190. /*---------------------------------------------------------------------------*/
  191.  
  192. static BOOL initialize_instance( HANDLE hInstance, int nCmdShow )
  193. {
  194.     HWND hWnd;
  195.     int ht_border, ht_caption, ht_menu, wd_border;
  196.  
  197.     hInst = hInstance;
  198.  
  199.     wd_border  = GetSystemMetrics( SM_CXBORDER );
  200.     ht_border  = GetSystemMetrics( SM_CYBORDER );
  201.     ht_caption = GetSystemMetrics( SM_CYCAPTION );
  202.     ht_menu    = GetSystemMetrics( SM_CYMENU );
  203.  
  204.     hBitmap = LoadBitmap( hInstance, "WEDL" );
  205.     GetObject( hBitmap, sizeof( BITMAP ), (LPSTR) &bitmap );
  206.  
  207.     hWnd = CreateWindow(
  208.                 szClassName, "WEDL Demonstration Program",
  209.                 WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION  | WS_MINIMIZEBOX,
  210.                 65, 35, bitmap.bmWidth + wd_border + wd_border,
  211.                 bitmap.bmHeight + ht_border + ht_border + ht_caption + ht_menu,
  212.                 NULL, NULL, hInstance, NULL );
  213.  
  214.     if( hWnd == NULL ) return( FALSE );
  215.  
  216.     ShowWindow( hWnd, nCmdShow );
  217.     UpdateWindow( hWnd );
  218.     return( TRUE );
  219. }
  220.  
  221. /*---------------------------------------------------------------------------*/
  222.  
  223. BOOL FAR PASCAL error_func( HFORM hform, HFIELD hfield, int error_value,
  224.                             int error_position, int error_event )
  225. {
  226.     HWND hDlg;
  227.  
  228.     hDlg = form_get_hdlg( hform );
  229.     switch( error_value ) {
  230.         case BAD_DATE:
  231.             MessageBox( hDlg, "Date Is Invalid", NULL, MB_OK );
  232.             break;
  233.         case BAD_STATE:
  234.             MessageBox( hDlg, "Invalid State Code", NULL, MB_OK );
  235.             break;
  236.         case BAD_ZIP:
  237.             MessageBox( hDlg, error_position > 1 ? "Zip Code is incomplete"
  238.                         : "Zip code is invalid for given State", NULL, MB_OK );
  239.             break;
  240.         case BAD_FILENAME:
  241.             MessageBox( hDlg, "File Not In Directory", NULL, MB_OK );
  242.             break;
  243.         default:
  244.             return( FALSE );        /* error wasn't handled */
  245.     }
  246.     return( TRUE );                 /* error was handled */
  247. }
  248.  
  249. /*****************************************************************************/
  250. /*---------------------------------------------------------------------------*/
  251. /*---------------------------------------------------------------------------*/
  252. /*------------------------/                           \----------------------*/
  253. /*------------------------ Employee Information Dialog ----------------------*/
  254. /*------------------------\                           /----------------------*/
  255. /*---------------------------------------------------------------------------*/
  256. /*---------------------------------------------------------------------------*/
  257. /*****************************************************************************/
  258.  
  259. #define IDHELP 998
  260.  
  261. /*---------------------------------------------------------------------------*/
  262.  
  263. /* state/ZIP code table */
  264.  
  265. struct states_t {
  266.     PSTR state_code;
  267.     int zip_low;
  268.     int zip_high;
  269. };
  270.  
  271. static struct states_t states[] = {
  272.     { "AK", 995, 999 },     /* Alaska               */
  273.     { "AL", 350, 369 },     /* Alabama              */
  274.     { "AR", 716, 729 },     /* Arkansas             */
  275.     { "AZ", 850, 865 },     /* Arizona              */
  276.     { "CA", 900, 961 },     /* California           */
  277.     { "CO", 800, 816 },     /* Colorado             */
  278.     { "CT",  60,  69 },     /* Connecticut          */
  279.     { "DE", 197, 199 },     /* Delaware             */
  280.     { "FL", 320, 349 },     /* Florida              */
  281.     { "GA", 300, 319 },     /* Georgia              */
  282.     { "HI", 967, 968 },     /* Hawaii               */
  283.     { "IA", 500, 528 },     /* Iowa                 */
  284.     { "ID", 832, 847 },     /* Idaho                */
  285.     { "IL", 600, 629 },     /* Illinois             */
  286.     { "IN", 460, 479 },     /* Indiana              */
  287.     { "KS", 641, 679 },     /* Kansas               */
  288.     { "KY", 400, 427 },     /* Kentucky             */
  289.     { "LA", 700, 714 },     /* Louisiana            */
  290.     { "MA",  10,  27 },     /* Massachusetts        */
  291.     { "MD", 206, 219 },     /* Maryland             */
  292.     { "ME",  39,  49 },     /* Maine                */
  293.     { "MI", 480, 499 },     /* Michigan             */
  294.     { "MN", 550, 567 },     /* Minnesota            */
  295.     { "MO", 630, 658 },     /* Missouri             */
  296.     { "MS", 386, 397 },     /* Mississippi          */
  297.     { "MT", 590, 599 },     /* Montana              */
  298.     { "NC", 270, 289 },     /* North Carolina       */
  299.     { "ND", 580, 588 },     /* North Dakota         */
  300.     { "NE", 680, 693 },     /* Nebraska             */
  301.     { "NH",  30,  38 },     /* New Hampshire        */
  302.     { "NJ",  70,  89 },     /* New Jersey           */
  303.     { "NM", 870, 884 },     /* New Mexico           */
  304.     { "NV", 889, 898 },     /* Nevada               */
  305.     { "NY", 100, 149 },     /* New York             */
  306.     { "OH", 430, 458 },     /* Ohio                 */
  307.     { "OK", 730, 749 },     /* Oklahoma             */
  308.     { "OR", 970, 979 },     /* Oregon               */
  309.     { "PA", 150, 196 },     /* Pennsylvania         */
  310.     { "RI",  27,  29 },     /* Rhode Island         */
  311.     { "SC", 290, 299 },     /* South Carolina       */
  312.     { "SD", 570, 577 },     /* South Dakota         */
  313.     { "TN", 370, 385 },     /* Tennessee            */
  314.     { "TX", 750, 885 },     /* Texas                */
  315.     { "UT", 840, 847 },     /* Utah                 */
  316.     { "VA", 220, 246 },     /* Virginia             */
  317.     { "VT",  50,  59 },     /* Vermont              */
  318.     { "WA", 980, 994 },     /* Washington           */
  319.     { "WI", 530, 549 },     /* Wisconsin            */
  320.     { "WV", 247, 268 },     /* West Virginia        */
  321.     { "WY", 820, 831 },     /* Wyoming              */
  322.     { "DC", 200, 205 },     /* District of Columbia */
  323.     { "GU",   0, 999 },     /* Guam                 */
  324.     { "PR",   0, 999 },     /* Puerto Rico          */
  325.     { "VI",   0, 999 },     /* Virgin Islands       */
  326.     { "ON",   0, 999 },     /* Ontario              */
  327.     { "PQ",   0, 999 },     /* Quebec               */
  328.     { "BC",   0, 999 },     /* British Columbia     */
  329.     { "AB",   0, 999 },     /* Alberta              */
  330.     { "SK",   0, 999 },     /* Saskatchewan         */
  331.     { "MB",   0, 999 },     /* Manitoba             */
  332.     { "NS",   0, 999 },     /* Nova Scotia          */
  333.     { "NB",   0, 999 },     /* New Brunswick        */
  334.     { "NF",   0, 999 },     /* Newfoundland         */
  335.     { "PE",   0, 999 },     /* Prince Edward Island */
  336.     { NULL,   0,   0 }      /* --- end of table --- */
  337. };
  338.  
  339. /*---------------------------------------------------------------------------*/
  340.  
  341. int FAR PASCAL EmployeeDlgProc( HWND hDlg, unsigned message, WORD wParam,
  342.                                 LONG lParam )
  343. {
  344.     static HFORM hform = NULL;
  345.     static char first_name[16]="", mid_init[2]="", last_name[21]="";
  346.     static char address[31]="", city[16]="", state[3]="", zip_code[10]="";
  347.     static char soc_sec_no[10]="", phone_num[11]="", hire_date[9]="";
  348.     static double wage = 0.0;
  349.  
  350.     switch( message ) {
  351.         case WM_INITDIALOG:
  352.             hform = form_begin( hDlg, FMF_NOSELECT | FMF_VKEYPRES |
  353.                                 FMF_VLEAVFLD | FMF_UPDATE | FMF_OVERTYPE,
  354.                                 perror_func );
  355.             form_set_help( hform, "demohelp.hlp", 0 );
  356.             field_define( hform, IDD_SSN, soc_sec_no, FDT_STRING,
  357.                           "<0..7>99'-'99'-'9(4)", FDF_NOTBLANK | FDF_COMPLETE,
  358.                           NULL, 0, IDH_SSN );
  359.             field_define( hform, IDD_FNAME, first_name, FDT_STRING,
  360.                           "A(15)", FDF_PROPER,
  361.                           NULL, 0, IDH_FNAME );
  362.             field_define( hform, IDD_MIDINIT, mid_init, FDT_STRING,
  363.                           "A(1)'.'",  FDF_UPPER, NULL, 0, IDH_MIDINIT );
  364.             field_define( hform, IDD_LNAME, last_name, FDT_STRING,
  365.                           "<A..Z>A(19)", FDF_PROPER,
  366.                           NULL, 0, IDH_LNAME );
  367.             field_define( hform, IDD_ADDRESS, address, FDT_STRING,
  368.                           "?(30)", FDF_PROPER,
  369.                           NULL, 0, IDH_ADDRESS );
  370.             field_define( hform, IDD_CITY, city, FDT_STRING,
  371.                           "?(15)", FDF_PROPER,
  372.                           NULL, 0, IDH_CITY );
  373.             field_define( hform, IDD_STATE, state, FDT_STRING,
  374.                           "A(2)", FDF_COMPLETE | FDF_UPPER,
  375.                           pcheck_state, BAD_STATE, IDH_STATE );
  376.             field_define( hform, IDD_ZIP, zip_code, FDT_STRING,
  377.                           "<0..9>(5)'-'9(4)", FDF_NONE,
  378.                           pcheck_zip_code, BAD_ZIP, IDH_ZIP );
  379.             field_define( hform, IDD_PHONE, phone_num, FDT_STRING,
  380.                           "'('999') '999'-'9999", FDF_COMPLETE,
  381.                           NULL, 0, IDH_PHONE );
  382.             field_define( hform, IDD_HIREDATE, hire_date, FDT_STRING,
  383.                           " <01> 9 / <0123> 9 / <89> 9 ", FDF_COMPLETE |
  384.                           FDF_PHYSICAL, pcheck_date, BAD_DATE, IDH_HIREDATE );
  385.             field_define( hform, IDD_WAGE, &wage, FDT_DOUBLE,
  386.                           "'$'999999.99", FDF_NUMERIC | FDF_BLNKZERO |
  387.                           FDF_BLNKNEZ, NULL, 0, IDH_WAGE );
  388.             keystat_define( hform, IDD_INSERT, KSM_INSERT, "Insert: On",
  389.                             "Insert: Off" );
  390.             generic_define( hform, IDHELP, 0 );
  391.             form_end( hform );
  392.             return( TRUE );
  393.  
  394.         case WM_COMMAND:
  395.             switch( wParam ) {
  396.                 case IDOK:
  397.                     form_ok( hform );
  398.                     EndDialog( hDlg, TRUE );
  399.                     sprintf( tbuf, "Soc Sec No.\t= %s\n"
  400.                              "Name\t\t= %s %s. %s\nAddress\t\t= %s\n"
  401.                              "\t\t= %s, %s %.5s-%s\nPhone No.\t= %s\n"
  402.                              "Hire Date\t= %s\nWage/Salary\t= %.2lf",
  403.                              soc_sec_no, first_name, mid_init, last_name,
  404.                              address, city, state, zip_code, zip_code+5,
  405.                              phone_num, hire_date, wage );
  406.                     MessageBox( NULL, tbuf, "Field Contents", MB_OK );
  407.                     return( TRUE );
  408.                 case IDCANCEL:
  409.                     form_cancel( hform );
  410.                     EndDialog( hDlg, TRUE );
  411.                     return( TRUE );
  412.                 case IDHELP:
  413.                     WinHelp( hDlg, "demohelp.hlp", HELP_INDEX, NULL );
  414.                     return( TRUE );
  415.             }
  416.             break;
  417.  
  418.         case WM_CLOSE:
  419.             SendMessage( hDlg, WM_COMMAND, IDCANCEL, 0 );
  420.             return( TRUE );
  421.     }
  422.     return( FALSE );
  423. }
  424.  
  425. /*---------------------------------------------------------------------------*/
  426.  
  427. int FAR PASCAL check_date( HFORM hform, HFIELD hfield, LPSTR pbuf )
  428. {
  429.     char date[10];
  430.     int month, day, year;
  431.  
  432.     if( !str_is_blank( pbuf ) ) {
  433.  
  434.         /* parse year, day, and month from buffer */
  435.         lstrcpy( date, pbuf );
  436.         year = 1900 + atoi( date+4 );
  437.         *( date+4 ) = '\0';
  438.         day = atoi( date+2 );
  439.         *( date+2 ) = '\0';
  440.         month = atoi( date );
  441.  
  442.         /* validate month */
  443.         if( month > 12 ) return( 1 );
  444.  
  445.         /* validate day and month */
  446.         if( day < 1 ) return( 3 );
  447.         switch( month ) {
  448.             case 2:
  449.                 if( year%4 ) {
  450.                     if( day > 29 ) return( 3 );
  451.                 }
  452.                 else {
  453.                     if( day > 28 ) return( 3 );
  454.                 }
  455.                 break;
  456.             case 1:
  457.             case 3:
  458.             case 5:
  459.             case 7:
  460.             case 8:
  461.             case 10:
  462.             case 12:
  463.                 if( day > 31 ) return( 3 );
  464.                 break;
  465.             case 4:
  466.             case 6:
  467.             case 9:
  468.             case 11:
  469.                 if( day > 30 ) return( 3 );
  470.                 break;
  471.             default:
  472.                 return( 1 );
  473.         }
  474.     }
  475.  
  476.     /* return zero indicating no error */
  477.     return( 0 );
  478. }
  479.  
  480. /*---------------------------------------------------------------------------*/
  481.  
  482. int FAR PASCAL check_state( HFORM hform, HFIELD hfield, LPSTR pbuf )
  483. {
  484.     int i;
  485.  
  486.     /* allow state to be blank */
  487.     if( str_is_blank( pbuf ) ) return( 0 );
  488.  
  489.     /* do for all state codes in the table */
  490.     for( i = 0 ; states[i].state_code != NULL ; i++ )
  491.  
  492.         /* if state code was found, then no error */
  493.         if( !lstrcmp( states[i].state_code, pbuf ) ) return( 0 );
  494.  
  495.     /* not a legal 2-letter state code */
  496.     return( 1 );
  497. }
  498.  
  499. /*---------------------------------------------------------------------------*/
  500.  
  501. int FAR PASCAL check_zip_code( HFORM hform, HFIELD hfield, LPSTR pbuf )
  502. {
  503.     LPSTR p;
  504.     int i, num_spaces;
  505.     long zip, zip_low, zip_high;
  506.  
  507.     /* allow zip code to be blank */
  508.     if( str_is_blank( pbuf ) ) return( 0 );
  509.  
  510.     /* count spaces in the extended portion of the 9-digit zip code */
  511.     num_spaces = 0;
  512.     for( p = pbuf + 5 ; *p != '\0' ; p++ ) if( *p == ' ' ) num_spaces++;
  513.  
  514.     /* if zip code isn't exactly 5 or 9 digits, then there's an error */
  515.     if( num_spaces != 0 && num_spaces != 4 ) return( 6 );
  516.  
  517.     *( pbuf+5 ) = '\0';
  518.     field_log_to_data( hfield, pbuf, &zip, FDT_LONG );
  519.  
  520.     /* find matching state */
  521.     hfield = field_get_from_ctrl_id( hform, IDD_STATE );
  522.     field_get_text( hfield, tbuf, FALSE );
  523.     for( i = 0 ; states[i].state_code != NULL ; i++ ) {
  524.         if( !lstrcmp( tbuf, states[i].state_code ) ) break;
  525.     }
  526.     if( states[i].state_code == NULL ) return( 0 );
  527.  
  528.     /* test zip code */
  529.     zip_low  = ( (long) ( states[i].zip_low ) ) * 100L;
  530.     zip_high = ( (long) ( states[i].zip_high ) ) * 100L;
  531.     if( zip >= zip_low && zip <= zip_high ) return( 0 );
  532.     return( 1 );
  533. }
  534.  
  535. /*****************************************************************************/
  536. /*---------------------------------------------------------------------------*/
  537. /*---------------------------------------------------------------------------*/
  538. /*------------------------/                          \-----------------------*/
  539. /*------------------------    Album Database Dialog   -----------------------*/
  540. /*------------------------\                          /-----------------------*/
  541. /*---------------------------------------------------------------------------*/
  542. /*---------------------------------------------------------------------------*/
  543. /*****************************************************************************/
  544.  
  545. /* prototypes for Album Database dialog */
  546.  
  547. static void fill_media_list_box( HWND hDlg );
  548. static int get_record( int recno );
  549. static BOOL okay_to_save_changes( HWND hDlg );
  550. static void update_record( int recno );
  551.  
  552. /*---------------------------------------------------------------------------*/
  553.  
  554. /* albums "file" */
  555.  
  556. struct albums_t {
  557.     char artist[25+1];
  558.     char title[30+1];
  559.     char label[15+1];
  560.     int copyright;
  561.     char media[15+1];
  562. };
  563.  
  564. static struct albums_t albums[] = {
  565.     { "Aerosmith", "Pump", "Geffen", 1989, "Compact Disc" },
  566.     { "Bell Biv Devoe", "Poison", "MCA", 1990, "Cassette" },
  567.     { "The Cure", "Disintegration", "Elektra", 1989, "Compact Disc" },
  568.     { "Front 242", "Front by Front", "Wax Trax", 1988, "Compact Disc" },
  569.     { "INXS", "Kick", "Atlantic", 1987, "Cassette" },
  570.     { "Janet Jackson", "Control", "A&M", 1986, "Cassette" },
  571.     { "Kenny G", "Live", "Arista", 1989, "Compact Disc" },
  572.     { "R.E.M.", "Out of Time", "Warner Bros.", 1991, "Cassette" },
  573.     { "Sex Pistols", "Never Mind the Bollocks", "Warner Bros.", 1977, "Record" },
  574.     { "Van Halen", "OU812", "Warner Bros.", 1988, "Compact Disc" },
  575. };
  576.  
  577. /*---------------------------------------------------------------------------*/
  578.  
  579. /* buffer area to use for editing Albums "file" */
  580.  
  581. static char artist[25+1];
  582. static char title[30+1];
  583. static char label[15+1];
  584. static int  copyright;
  585. static char media[15+1];
  586.  
  587. /*---------------------------------------------------------------------------*/
  588.  
  589. /* media type table */
  590.  
  591. static PSTR media_types[] = { "Compact Disc", "Cassette", "Record", "DAT",
  592.                               "Mini Disc", "DCC", "Open Reel", "8-Track",
  593.                               NULL };
  594.  
  595. /*---------------------------------------------------------------------------*/
  596.  
  597. int FAR PASCAL AlbumDlgProc( HWND hDlg, unsigned message, WORD wParam,
  598.                              LONG lParam )
  599. {
  600.     static HFORM hform = NULL;
  601.     static int curr_rec = 0;
  602.     int new_rec;
  603.  
  604.     switch( message ) {
  605.         case WM_INITDIALOG:
  606.             curr_rec = get_record( 1 );
  607.             hform = form_begin( hDlg, FMF_UPDATE, NULL );
  608.             form_set_help( hform, "demohelp.hlp", 0 );
  609.             field_define( hform, IDD_RECNO, &curr_rec, FDT_INTEGER, "999",
  610.                           FDF_NOTEDIT, NULL, 0, IDH_RECNO );
  611.             field_define( hform, IDD_ARTIST, artist, FDT_STRING, "?(25)",
  612.                           FDF_NOTBLANK, NULL, 0, IDH_ARTIST );
  613.             field_define( hform, IDD_TITLE, title, FDT_STRING, "?(30)",
  614.                           FDF_NONE, NULL, 0, IDH_TITLE );
  615.             field_define( hform, IDD_LABEL, label, FDT_STRING, "?(15)",
  616.                           FDF_NONE, NULL, 0, IDH_LABEL );
  617.             field_define( hform, IDD_COPYRIGHT, ©right, FDT_INTEGER, "<1><9>99",
  618.                           FDF_COMPLETE, NULL, 0, IDH_COPR );
  619.             field_define( hform, IDD_MEDIA, media, FDT_STRING, "?(15)",
  620.                           FDF_COMBO | FDF_NOTBLANK, NULL, 0, IDH_MEDIA );
  621.             keystat_define( hform, IDD_INSERT,   KSM_INSERT,   "[Ins]",  "" );
  622.             keystat_define( hform, IDD_CAPSLOCK, KSM_CAPSLOCK, "[Caps]", "" );
  623.             keystat_define( hform, IDD_NUMLOCK,  KSM_NUMLOCK,  "[Num]",  "" );
  624.             form_end( hform );
  625.             fill_media_list_box( hDlg );
  626.             return( TRUE );
  627.  
  628.         case WM_COMMAND:
  629.             switch( wParam ) {
  630.                 case IDOK:
  631.                     form_ok( hform );
  632.                     EndDialog( hDlg, TRUE );
  633.                     return( TRUE );
  634.                 case IDCANCEL:
  635.                     form_cancel( hform );
  636.                     EndDialog( hDlg, TRUE );
  637.                     return( TRUE );
  638.                 case IDD_PREVIOUS:
  639.                 case IDD_NEXT:
  640.                     if( form_validate( hform ) != NULL ) return( TRUE );
  641.                     if( form_has_changed( hform ) ) {
  642.                         if( okay_to_save_changes( hDlg ) ) {
  643.                             form_save( hform );
  644.                             update_record( curr_rec );
  645.                         }
  646.                     }
  647.                     new_rec = ( wParam == IDD_PREVIOUS )
  648.                                  ? ( curr_rec - 1 ) : ( curr_rec + 1 );
  649.                     new_rec = get_record( new_rec );
  650.                     if( new_rec == -1 )
  651.                         MessageBeep( 0 );
  652.                     else
  653.                         curr_rec = new_rec;
  654.                     form_load( hform );
  655.                     PostMessage( hDlg, WM_COMMAND, GOTO_ARTIST, 0 );
  656.                     return( TRUE );
  657.                 case GOTO_ARTIST:
  658.                     SetFocus( GetDlgItem( hDlg, IDD_ARTIST ) );
  659.                     return( TRUE );
  660.             }
  661.             break;
  662.         case WM_CLOSE:
  663.             SendMessage( hDlg, WM_COMMAND, IDCANCEL, 0 );
  664.             return( TRUE );
  665.     }
  666.     return( FALSE );
  667. }
  668.  
  669. /*---------------------------------------------------------------------------*/
  670.  
  671. static void fill_media_list_box( HWND hDlg )
  672. {
  673.     int i;
  674.     for( i = 0 ; media_types[i] != NULL ; i++ ) {
  675.         SendMessage( GetDlgItem( hDlg, IDD_MEDIA ), CB_ADDSTRING, 0,
  676.                      (LONG) ( (LPSTR) media_types[i] ) );
  677.     }
  678. }
  679.  
  680. /*---------------------------------------------------------------------------*/
  681.  
  682. /* gets a record from the Albums "file" */
  683.  
  684. static int get_record( int recno )
  685. {
  686.     int last_rec, ofs;
  687.  
  688.     last_rec = sizeof( albums ) / sizeof( albums[0] );
  689.     if( recno < 1 || recno > last_rec ) return( -1 );
  690.     ofs = recno - 1;
  691.     lstrcpy( artist, albums[ofs].artist );
  692.     lstrcpy( title,  albums[ofs].title );
  693.     lstrcpy( label,  albums[ofs].label );
  694.     copyright = albums[ofs].copyright;
  695.     lstrcpy( media,  albums[ofs].media );
  696.     return( recno );
  697. }
  698.  
  699. /*---------------------------------------------------------------------------*/
  700.  
  701. static BOOL okay_to_save_changes( HWND hDlg )
  702. {
  703.     return( MessageBox( hDlg, "Record has changed.  Save changes?",
  704.                         "Record Has Changed",
  705.                         MB_ICONQUESTION | MB_YESNO ) == IDYES );
  706. }
  707.  
  708. /*---------------------------------------------------------------------------*/
  709.  
  710. /* updates a record in the Albums "file" */
  711.  
  712. static void update_record( int recno )
  713. {
  714.     int ofs;
  715.  
  716.     ofs = recno - 1;
  717.     lstrcpy( albums[ofs].artist, artist );
  718.     lstrcpy( albums[ofs].title, title );
  719.     lstrcpy( albums[ofs].label, label );
  720.     albums[ofs].copyright = copyright;
  721.     lstrcpy( albums[ofs].media, media );
  722. }
  723.  
  724. /*****************************************************************************/
  725. /*---------------------------------------------------------------------------*/
  726. /*---------------------------------------------------------------------------*/
  727. /*------------------------/                          \-----------------------*/
  728. /*------------------------    Upload File Dialog      -----------------------*/
  729. /*------------------------\                          /-----------------------*/
  730. /*---------------------------------------------------------------------------*/
  731. /*---------------------------------------------------------------------------*/
  732. /*****************************************************************************/
  733.  
  734. /* prototypes for Upload File dialog */
  735.  
  736. static void fill_list_boxes( HWND hDlg, PSTR pathspec );
  737.  
  738. /*---------------------------------------------------------------------------*/
  739.  
  740. /* protocol name table */
  741.  
  742. static PSTR protocols[] = { "Xmodem", "Ymodem", "Zmodem" };
  743.  
  744. /*---------------------------------------------------------------------------*/
  745.  
  746. int FAR PASCAL UploadDlgProc( HWND hDlg, unsigned message, WORD wParam,
  747.                               LONG lParam )
  748. {
  749.     static HFORM hform = NULL;
  750.     static int protocol = 0, hangup;
  751.     static char path_spec[81], file_name[13];
  752.     static HFIELD hfile_name;
  753.  
  754.     switch( message ) {
  755.         case WM_INITDIALOG:
  756.             hform = form_begin( hDlg, FMF_NONE, perror_func );
  757.             form_set_help( hform, "demohelp.hlp", 0 );
  758.             hfile_name = field_define( hform, IDD_FILENAME, file_name,
  759.                                        FDT_STRING, "F(12)", FDF_NOTBLANK,
  760.                                        pcheck_file_name, BAD_FILENAME,
  761.                                        IDH_FILENAME );
  762.             form_set_enable_link( hform, (HANDLE) hfile_name, ELC_BLANK, IDOK,
  763.                                   FALSE );
  764.             generic_define( hform, IDD_FILEBOX, IDH_FILEBOX );
  765.             generic_define( hform, IDD_DIRBOX, IDH_DIRBOX );
  766.             button_define( hform, IDD_XMODEM, &protocol, 1, 0, 0,
  767.                            BTF_UPDATE, IDH_XMODEM );
  768.             button_define( hform, IDD_YMODEM, &protocol, 1, 1, 0,
  769.                            BTF_UPDATE, IDH_YMODEM );
  770.             button_define( hform, IDD_ZMODEM, &protocol, 1, 2, 0,
  771.                            BTF_UPDATE, IDH_ZMODEM );
  772.             button_define( hform, IDD_HANGUP, &hangup, 0, TRUE, FALSE,
  773.                            BTF_NONE, IDH_HANGUP );
  774.             form_end( hform );
  775.             fill_list_boxes( hDlg, "*.*" );
  776.             return( TRUE );
  777.  
  778.         case WM_COMMAND:
  779.             switch( wParam ) {
  780.                 case IDOK:
  781.                     form_ok( hform );
  782.                     GetDlgItemText( hDlg, IDD_PATHSPEC, path_spec, 80 );
  783.                     EndDialog( hDlg, TRUE );
  784.                     wsprintf( tbuf, "Uploading the file:  %s%s%s\n"
  785.                               "Using the %s protocol.\n"
  786.                               "Will %shang up after transfer.",
  787.                               (LPSTR) path_spec,
  788.                               *( path_spec + lstrlen( path_spec ) - 1 )
  789.                               == '\\' ? (LPSTR) "" : (LPSTR) "\\",
  790.                               (LPSTR) file_name,
  791.                               (LPSTR) protocols[protocol],
  792.                               hangup ? (LPSTR) "" : (LPSTR) "not " );
  793.                     MessageBox( NULL, tbuf, "Upload File", MB_OK );
  794.                     return( TRUE );
  795.  
  796.                 case IDCANCEL:
  797.                     form_cancel( hform );
  798.                     EndDialog( hDlg, TRUE );
  799.                     return( TRUE );
  800.  
  801.                 case IDD_FILEBOX:
  802.                     if( HIWORD( lParam ) == LBN_SELCHANGE ) {
  803.                         SendDlgItemMessage( hDlg, IDD_FILEBOX, LB_GETTEXT,
  804.                                             (int) SendDlgItemMessage( hDlg,
  805.                                             IDD_FILEBOX, LB_GETCURSEL, 0, 0 ),
  806.                                             (LONG) ( (LPSTR) tbuf ) );
  807.                         field_set_text( hfile_name, tbuf, TRUE );
  808.                         return( TRUE );
  809.                     }
  810.                     if( HIWORD( lParam ) == LBN_DBLCLK ) {
  811.                         PostMessage( hDlg, WM_COMMAND, IDOK,
  812.                                      (LONG) GetDlgItem( hDlg, IDOK ) );
  813.                         return( TRUE );
  814.                     }
  815.                     break;
  816.  
  817.                 case IDD_DIRBOX:
  818.                     if( HIWORD( lParam ) == LBN_DBLCLK ) {
  819.                         DlgDirSelect( hDlg, path_spec, IDD_DIRBOX );
  820.                         field_set_text( hfile_name, "", TRUE );
  821.                         lstrcat( path_spec, "*.*" );
  822.                         fill_list_boxes( hDlg, path_spec );
  823.                         return( TRUE );
  824.                     }
  825.                     break;
  826.             }
  827.             break;
  828.         case WM_CLOSE:
  829.             SendMessage( hDlg, WM_COMMAND, IDCANCEL, 0 );
  830.             return( TRUE );
  831.     }
  832.     return( FALSE );
  833. }
  834.  
  835. /*---------------------------------------------------------------------------*/
  836.  
  837. int FAR PASCAL check_file_name( HFORM hform, HFIELD hfield, LPSTR pbuf )
  838. {
  839.     char file_name[13];
  840.  
  841.     lstrcpy( file_name, pbuf );
  842.     str_trim_spaces( file_name );
  843.     return( ( SendDlgItemMessage( GetParent( GetFocus() ), IDD_FILEBOX,
  844.             LB_FINDSTRING, (WPARAM) -1, (LONG) ( (LPSTR) file_name ) ) == LB_ERR )
  845.             ? 1 : 0 );
  846. }
  847.  
  848. /*---------------------------------------------------------------------------*/
  849.  
  850. static void fill_list_boxes( HWND hDlg, PSTR pathspec )
  851. {
  852.     DlgDirList( hDlg, pathspec, IDD_FILEBOX, 0, 0x0001 | 0x0020 );
  853.     DlgDirList( hDlg, pathspec, IDD_DIRBOX, IDD_PATHSPEC, 0x0010 | 0x4000 |
  854.                 0x8000 );
  855. }
  856.  
  857. /*****************************************************************************/
  858. /*---------------------------------------------------------------------------*/
  859. /*---------------------------------------------------------------------------*/
  860. /*------------------------/                          \-----------------------*/
  861. /*------------------------   Numeric Fields Dialog     ----------------------*/
  862. /*------------------------\                          /-----------------------*/
  863. /*---------------------------------------------------------------------------*/
  864. /*---------------------------------------------------------------------------*/
  865. /*****************************************************************************/
  866.  
  867. int FAR PASCAL NumericDlgProc( HWND hDlg, unsigned message, WORD wParam,
  868.                               LONG lParam )
  869. {
  870.     static HFORM hform = NULL;
  871.     static long std = 0;
  872.     static unsigned long zerofill = 0;
  873.     static unsigned int calc = 0;
  874.     static double stddec = 0.0, calcdec = 0.0, real= 0.0;
  875.  
  876.     switch( message ) {
  877.  
  878.         case WM_INITDIALOG:
  879.             hform = form_begin( hDlg, FMF_OVERTYPE | FMF_NOSELECT | FMF_UPDATE,
  880.                                 NULL );
  881.             field_define( hform, IDD_STD, &std, FDT_LONG,
  882.                           "N(7)", FDF_NUMERIC, NULL, 0, 0 );
  883.             field_define( hform, IDD_STDDEC, &stddec, FDT_DOUBLE,
  884.                           "9(5).99", FDF_NUMERIC, NULL, 0, 0 );
  885.             field_define( hform, IDD_ZEROFILL, &zerofill, FDT_ULONG,
  886.                           "9(9)", FDF_NUMERIC | FDF_ZEROFILL, NULL, 0, 0 );
  887.             field_define( hform, IDD_CALC, &calc, FDT_UNSIGNED,
  888.                           "<0123456 >9(4)", FDF_CALCNUM | FDF_VLEAVFLD,
  889.                           NULL, 0, 0 );
  890.             field_define( hform, IDD_CALCDEC, &calcdec, FDT_DOUBLE,
  891.                           "N,NNN,NNN.NN", FDF_CALCNUM, NULL, 0, 0 );
  892.             field_define( hform, IDD_REAL, &real, FDT_DOUBLE,
  893.                           "#(9)", FDF_NUMERIC, NULL, 0, 0 );
  894.             keystat_define( hform, IDD_INSERT, KSM_INSERT, "(Ins)", "(Ovr)" );
  895.             form_end( hform );
  896.             return( TRUE );
  897.  
  898.         case WM_COMMAND:
  899.             switch( wParam ) {
  900.                 case IDOK:
  901.                     form_ok( hform );
  902.                     EndDialog( hDlg, TRUE );
  903.                     sprintf( tbuf, "%ld\t%.2lf\n%lu\t%u\n%.2lf\n%le\n", std,
  904.                              stddec, zerofill, calc, calcdec, real );
  905.                     MessageBox( NULL, tbuf, "Field Contents", MB_OK );
  906.                     return( TRUE );
  907.                 case IDCANCEL:
  908.                     form_cancel( hform );
  909.                     EndDialog( hDlg, TRUE );
  910.                     return( TRUE );
  911.             }
  912.             break;
  913.  
  914.         case WM_CLOSE:
  915.             SendMessage( hDlg, WM_COMMAND, IDCANCEL, 0 );
  916.             return( TRUE );
  917.  
  918.     }
  919.     return( FALSE );
  920. }
  921.  
  922.