home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / demoform.c < prev    next >
C/C++ Source or Header  |  1991-03-23  |  6KB  |  276 lines

  1. /* form demonstration program.
  2.  * This program sets up 2 different data entry form, 
  3.  *    one is easy (few fancy options)
  4.  *  one is harder (fancy options included).
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10.  
  11.  
  12.  
  13. #include "wtwg.h"
  14.  
  15.  
  16. static int    integer_data = 0;
  17. static float  float_data   = 0.0;
  18. static char   text_data[]  = "whatever you want";
  19. static char   date_data[11]     = "01/01/1989";
  20. static char   time_data[9]      = "00:00:00";
  21.  
  22.  
  23. WFORM easy_form[] = 
  24.     {
  25.     WFORMENTRY_F (float_data),     20, 2,        /* set x&y position of data */
  26.     WFORMENTRY_I (integer_data),   20, 3,
  27.     WFORMENTRY_S (text_data),      20, 4,
  28.     WFORMENTRY_DT(date_data),       20, 6,
  29.     WFORMENTRY_TM(time_data),      20, 7,
  30.  
  31.     WFORMENTRY_END(),              60, 10        /* width & height of form */
  32.     };
  33.  
  34.  
  35. /*--------------------------------------------------------------------*/
  36.  
  37.         /* setup a slightly more sophisticated form.
  38.          */
  39.  
  40.  
  41. char *choices[] =
  42.     {
  43.     "First",
  44.     "Second",
  45.     "Third",
  46.     "Fourth",
  47.     "Fifth",
  48.     "Sixth",
  49.     "Seventh",
  50.     "Eighth",
  51.     "Ninth",
  52.     "Tenth",
  53.     "Eleventh",
  54.     "Twelvth",
  55.     "Thirteenth",
  56.     "Fourteenth",
  57.     "Fifteenth",
  58.     "Sixteenth",
  59.     "Seventeenth",
  60.     "Eighteenth",
  61.     "Nineteenth",
  62.     "Twentieth",
  63.     "Twenty-First",
  64.     "Twenty-Second",
  65.     "Twenty-Third",
  66.     "Twenty-Fourth",
  67.     NULL
  68.     };
  69.  
  70.  
  71.  
  72.  
  73. char animal[40] = "Sheep", 
  74.      choice[10] = "Zero";
  75.  
  76. int  pos_int = 1;
  77. unsigned int hex = 0x01;
  78.  
  79.  
  80. /* special functions for validating custom data types in dext form.
  81.  */
  82. int no_cow (WFORM *form, char *buffer);
  83. int no_neg (WFORM *form, char *buffer);
  84.  
  85.  
  86. WFORM hard_form[] =
  87.     {
  88.     /* label              user    picklist  format, func,       len,      HOLD x    y  */ 
  89.      "POSITIVE INTEGER= ",&pos_int, NULL,   "%i",      no_neg,   3,     0,   20,  4
  90.     ,"HEXADECIMAL= ",     &hex, NULL,       "%x",    NULL,          7,     0,   20,  6
  91.     ,"A Label",              NULL,   NULL,     NULL,      NULL,          0,     0,   20,  8
  92.     ,"NOT A COW: ",       animal, NULL,     "%s",     no_cow, sizeof(animal),0, 20, 10
  93.     ,"SELECTION: ",       choice,choices,   "%s",     NULL,   sizeof(choice),0,20, 15
  94.     ,NULL,                NULL,   NULL,     NULL,      wfvall,            0,       0,40, 20
  95.     };
  96.  
  97.  
  98.  
  99.  
  100.  
  101. main ()
  102.     {
  103.     char key;
  104.  
  105.     int testmode;
  106.     int n;
  107.     char *user_choice;
  108.     char buffer[25];
  109.     
  110.     winit ('T');
  111.     
  112.     wclear();
  113.     testmode = wpromptc ( "> DEMO <", "Select a mode",
  114.                 "Text", "Graphics", NULL );
  115.  
  116.  
  117.     if ( testmode == 'G' )
  118.         {
  119.         winit ('G');
  120.         }
  121.     else
  122.     if ( testmode == ESCAPE )
  123.         {
  124.         exit (0);
  125.         }
  126.     whelp_install ( "demoform" );     /* pass name of help file */
  127.  
  128.     wmsdrag ();        /* install feature to make all windows draggable by mouse */
  129.  
  130.     wclear ();
  131.     wputs ( "DEMO of picklists, string input, and form input\n"
  132.             "    -click upper left corner of any window to move it with mouse\n"
  133.             "    -during string input, click upper right to set INSERT mode\n"
  134.             "         or use the INSERT key\n"
  135.             "    -right mouse button is equivalent to ESCAPE key\n"
  136.         );
  137.         
  138.     /* PART I - demo wpicklist () function for selecting 
  139.      * a string from a NULL-terminated list
  140.      * Note call to wsetlocation() forces wpicklist() location onscreen.
  141.      *    see wtwg.h for details on wsetlocation.
  142.      */
  143.     wsetlocation ( WLOC_ATXY, 3, 7 );
  144.     n = wpicklist ( "Pick One",  choices );
  145.     user_choice = choices [n];
  146.     
  147.     if ( user_choice == NULL )
  148.         {
  149.         user_choice = "You pressed ESCAPE";
  150.         }
  151.     else
  152.         {
  153.         wpromptc ( "YOUR CHOICE", user_choice, NULL );
  154.         }
  155.          
  156.          
  157.     /* PART II - demo wprompts() for single string input.
  158.      */     
  159.     buffer[0] =0;    /* make sure you don't prime wgets() with garbage data */
  160.     
  161.     wprompts ("PROMPT FOR TEXT", "Your name? ", buffer, sizeof(buffer));
  162.     
  163.     wpromptc (" YOU TYPED ", buffer, NULL );     
  164.          
  165.  
  166.     /* PART III - demonstrate form input using easy_form
  167.      */
  168.     wfm_autoskip = 0;    /* actually, 0 is default, this line not needed */
  169.     
  170.     wscanform( "AUTOSKIP OFF", easy_form );
  171.     
  172.     wfm_autoskip = WFM_AUTOSKIP; 
  173.     wscanform( "AUTOSKIP ON", easy_form );
  174.  
  175.     wopen ( 5,5, 60, 15, (RED<<4)+YELLOW, SPECKLE_BORDER,(RED<<4)+YELLOW, 0);
  176.     wtitle ( " form results " );
  177.     wprintf ("\ntext:%s, int:%i, float:%f, date:%s, time:%s",
  178.             text_data, integer_data, float_data, date_data, time_data);
  179.     wgetc();
  180.     wclose();
  181.         
  182.     wfm_autoskip = 0;    
  183.     
  184.     
  185.     /* PART III of demo: trickier form stuff 
  186.      */
  187.     if ( wmonitor == 'E' || wmonitor == 'V' ) 
  188.         {
  189.         /* splashy (ugly) color demo
  190.          */
  191.         wfmatr_lbl = (MAGENTA<<4)+YELLOW ;
  192.         wfmatr_lbl_active = (RED<<4)+YELLOW;
  193.         wfmatr_border = ( LIGHTRED<<4 )+YELLOW ;    /* BLINKS in text mode */
  194.         wfmatr_title = ( LIGHTGRAY <<4 )+RED;
  195.         wfmatr_data = (MAGENTA<<4) + LIGHTCYAN;
  196.         wfmatr_data_active = (CYAN<<4);
  197.         wfm_border_type = SPECKLE_BORDER;
  198.         }
  199.  
  200.     n = wscanform ( "any form title here", hard_form);
  201.  
  202.     wopen ( 2,2, wxabsmax-2, 5, (LIGHTGRAY<<4)+RED,
  203.         SINGLE_BORDER, (LIGHTGRAY<<4)+RED, WSAVE2RAM );
  204.     
  205.     wprintf ("YOU ENTERED name=%s, positive int data =%i, "
  206.              "choice=%s, hex data = %x; and you pressed %s",
  207.             animal,  pos_int, choice, hex,
  208.             (n == ESCAPE)? "ESCAPE" : "ENTER" );
  209.     wgetc();
  210.  
  211.     wclose();
  212.  
  213.     wprintform ("display only version", hard_form);
  214.     wgetc();
  215.  
  216.     wclose();    /* closes the window wopen'd by wprintform */
  217.  
  218.  
  219.     wclear();
  220.     wgoto ( wxabsmax/2 -3, wyabsmax/2);        /* approx. center */
  221.     wputs ("GOODBYE");
  222.  
  223.     wgetc();
  224.     
  225.     /* the next line forces the linker to include the floating point library
  226.      * wscanform uses f.p. library ONLY if float data is specified
  227.      *         but it is coded in a way that the linker won't necessarily recognize
  228.      *        as requiring f.p. (dynamically built arg list to vsprintf)
  229.      *        so if you have f.p. data in your form, 
  230.      *        your program MUST do some f.p. character conversion.
  231.      *
  232.      */
  233.     float_data = atof ("1.234");
  234.     wclear();
  235.  
  236.     return(0);    /* main */
  237.     }
  238.  
  239.  
  240.  
  241. /*  demo how to write any type of validation function for special form types.
  242.  *    This function rejects the word "COW"
  243.  */
  244. int no_cow (WFORM *form, char *buffer)
  245.     {
  246.     int rc =0;
  247.     if (memicmp (buffer, "cow", 3) == 0)
  248.         {
  249.         wform_showerror (form, "Illegal data -- \"cow\"");
  250.         strcpy ( buffer, "sheep");
  251.         rc = 1;
  252.         }
  253.     return (rc);    /* no_cow */
  254.     }
  255.  
  256.  
  257.  
  258. /* This function rejects negative numbers.
  259.  */
  260. int no_neg (WFORM *form, char *buffer)
  261.     {
  262.     int rc = 0;
  263.     if ( atoi (buffer) < 0)
  264.         {
  265.         wform_showerror (form, "No negative numbers, please.");
  266.         wform_showerror (form, NULL);
  267.         rc =1;
  268.         }
  269.     return (rc);    /* no_neg */
  270.     }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.