home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c070 / 2.ddi / TOOLS.2 / EXAMPLES / ENTRYEDT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-31  |  10.2 KB  |  347 lines

  1. /**
  2. *    ENTRYEDT.C    Sample use of multiple window oriented edit
  3. *            fields.
  4. *
  5. *  This program displays a window which contains several edit fields
  6. *  which comprise a simple customer information record, and requests
  7. *  input from the user.  The "state" field has a key access routine
  8. *  associated with it to ensure that the user only enters certain
  9. *  state names; the "zip" field has a key access routine which
  10. *  ensures that the user only enters digits.  The user transmits
  11. *  each field (and goes on to the next) by pressing Enter; the screen
  12. *  is transmitted when the user presses f10.
  13. *
  14. *  The command line format is as follows:
  15. *
  16. *    entryedt
  17. *
  18. *  The purpose of ENTRYEDT is to provide a working example of the
  19. *  proper method of the use of multiple edit fields in a window.
  20. *
  21. *  Version    6.00 (C)Copyright Blaise Computing Inc. 1989
  22. *
  23. **/
  24.  
  25. #include <bedit.h>
  26. #include <bwindow.h>
  27. #include <bstrings.h>
  28. #include <bkeys.h>
  29. #include <stdio.h>
  30.  
  31.  
  32.         /* Internal function prototypes.            */
  33. void main(void);
  34. void state_validation(KB_DATA *);
  35. void zip_validation(KB_DATA *);
  36.  
  37.         /* These constants define the number of columns     */
  38.         /* each field will have when it is displayed on the */
  39.         /* screen.                        */
  40. #define NAME_WIDTH    40
  41. #define ADDRESS_WIDTH    40
  42. #define CITY_WIDTH    20
  43. #define STATE_WIDTH    2
  44. #define ZIP_WIDTH    5
  45.  
  46.         /* Allocate storage for character buffers in which  */
  47.         /* the data for the fields will be returned.        */
  48. char name[NAME_WIDTH + 1]    = "";
  49. char address[ADDRESS_WIDTH + 1] = "";
  50. char city[CITY_WIDTH + 1]    = "";
  51. char state[STATE_WIDTH + 1]    = "";
  52. char zip[ZIP_WIDTH + 1]     = "";
  53.  
  54.  
  55. typedef struct            /* FIELD_DESCRIPTOR structure:        */
  56. {                /*   field info and data buffer.    */
  57.     ED_CONTROL    control;    /* Field control structure.        */
  58.     int     buffer_size;    /* Size of pbuffer in bytes.        */
  59.     char       *pbuffer;    /* Buffer in which to return data.  */
  60. } FIELD_DESCRIPTOR;
  61.  
  62.         /* screen_fields is an array of FIELD_DESCRIPTOR    */
  63.         /* structures, each of which describes one field on */
  64.         /* the screen.                        */
  65. FIELD_DESCRIPTOR screen_fields[] =
  66. {
  67.     {{{1, 14}, {1, NAME_WIDTH}, 0x74,        /* Name field.        */
  68.       {6, 7}, {0, 7}, 0, 0},
  69.      sizeof(name), name},
  70.     {{{2, 14}, {1, ADDRESS_WIDTH}, 0x74,    /* Address field.   */
  71.       {6, 7}, {0, 7}, 0, 0},
  72.      sizeof(address), address},
  73.     {{{3, 14}, {1, CITY_WIDTH}, 0x74,        /* City field.        */
  74.       {6, 7}, {0, 7}, 0, 0},
  75.      sizeof(city), city},
  76.     {{{4, 14}, {1, STATE_WIDTH}, 0x74,        /* State field.     */
  77.       {6, 7}, {0, 7}, state_validation, TOUP},
  78.      sizeof(state), state},
  79.     {{{5, 14}, {1, ZIP_WIDTH}, 0x74,        /* Zip field.        */
  80.       {6, 7}, {0, 7}, zip_validation, 0},
  81.      sizeof(zip), zip}
  82. };
  83.  
  84.  
  85. typedef struct        /* FIELD_LABEL structure: location & text   */
  86. {            /*   of one screen field label.         */
  87.     int   row;        /* The row at which to place the text.        */
  88.     int   col;        /* The column at which to place the text.   */
  89.     char *ptext;    /* The text to use as the field label.        */
  90. } FIELD_LABEL;
  91.  
  92.         /* field_labels is an array of FIELD_LABEL        */
  93.         /* structures, each of which contains the location  */
  94.         /* and text for one the label for one field.        */
  95. FIELD_LABEL field_labels[] =
  96. {
  97.     {1, 8, "Name:"},
  98.     {2, 5, "Address:"},
  99.     {3, 8, "City:"},
  100.     {4, 7, "State:    (AZ, CA, OR, WA or NV only)"},
  101.     {5, 9, "ZIP:"}
  102. };
  103.  
  104. BWINDOW *pwin;
  105.  
  106.  
  107. void main()
  108. {
  109.     int         error_code;
  110.     BORDER        bord;
  111.     WHERE        location;
  112.     int         mode,columns,act_page;
  113.     int         cursor_was_off,row,col,high,low;
  114.     KEY_SEQUENCE    key_sequence;
  115.     ED_ACTION        action;
  116.     ED_ACTION_LIST  key_action_list;
  117.     int         index;
  118.  
  119.         /* First, create the window and write the field     */
  120.         /* labels to their appropriate locations.        */
  121.     pwin = wncreate(8, 60, SC_CYAN);
  122.     if (pwin == NIL)
  123.     exit(-1);
  124.  
  125.     wnselect(pwin);
  126.  
  127.     for (index = 0;
  128.      index < sizeof(field_labels) / sizeof(field_labels[0]);
  129.      index++)
  130.     {
  131.     if (wnwrbuf(field_labels[index].row,
  132.             field_labels[index].col, 0,
  133.             field_labels[index].ptext,
  134.             -1, -1, CHARS_ONLY) == 0)
  135.     {
  136.         printf("Error %d writing to window.\n", b_wnerr);
  137.         exit(-1);
  138.     }
  139.     }
  140.  
  141.         /* Install the F10 key as a transmission key, which */
  142.         /* will be used as a signal that the user wishes to */
  143.         /* transmit the screen.                 */
  144.     key_sequence.character_code = KB_C_N_F10;
  145.     key_sequence.key_code    = KB_S_N_F10;
  146.     key_action_list.num_actions = 1;
  147.     action            = ED_TRANSMIT;
  148.     key_action_list.pactions    = &action;
  149.  
  150.     if (ED_NO_ERROR != edchgkey(&key_sequence, &key_action_list))
  151.     {
  152.     printf("Error %d adding keystroke.\n", b_wnerr);
  153.     exit(-1);
  154.     }
  155.  
  156.         /* Now set up the BORDER and WHERE structures.        */
  157.     bord.type        = BBRD_SSSS | BBRD_TCT;
  158.     bord.attr        = SC_MAGENTA;
  159.     bord.ttattr     = SC_CYAN;
  160.     bord.pttitle    = " Press f10 to transmit... ";
  161.  
  162.     location.dev    = scmode(&mode,&columns,&act_page);
  163.     location.page    = act_page;
  164.     location.corner.row = 10;
  165.     location.corner.col =  5;
  166.  
  167.         /* Remember the cursor size and state to restore    */
  168.         /* later, and display the window.            */
  169.     cursor_was_off = sccurst(&row,&col,&high,&low);
  170.  
  171.     if (NIL == wndsplay(pwin, &location, &bord))
  172.     {
  173.     printf("Error %d displaying window.\n", b_wnerr);
  174.     exit(-1);
  175.     }
  176.  
  177.         /* Now read the fields on the screen in sequence    */
  178.         /* until the user presses F10.                */
  179.     index = 0;
  180.     do
  181.     {
  182.     error_code = wnfield(pwin,
  183.                  screen_fields[index].pbuffer,
  184.                  screen_fields[index].pbuffer,
  185.                  screen_fields[index].buffer_size,
  186.                  &(screen_fields[index].control),
  187.                  &key_sequence);
  188.     index++;
  189.     if (index >= (sizeof(screen_fields) / sizeof(screen_fields[0])))
  190.         index = 0;
  191.     }
  192.     while ((key_sequence.character_code != KB_C_N_F10) ||
  193.        (key_sequence.key_code    != KB_S_N_F10));
  194.  
  195.     if (error_code != WN_NO_ERROR)
  196.     {
  197.     printf("Error %d editing in window.\n", error_code);
  198.     exit(-1);
  199.     }
  200.  
  201.     wnremove(pwin);
  202.     wndstroy(pwin);
  203.     if (b_wnerr)
  204.     {
  205.     printf("Error %d removing or destroying window.\n", b_wnerr);
  206.     exit(-1);
  207.     }
  208.  
  209.     sccurset(row, col);
  210.     scpgcur(cursor_was_off, high, low, CUR_NO_ADJUST);
  211.  
  212.     printf("Name:    \"%s\"\n", name);
  213.     printf("Address: \"%s\"\n", address);
  214.     printf("City:    \"%s\"\n", city);
  215.     printf("State:   \"%s\"\n", state);
  216.     printf("Zip:     \"%s\"\n", zip);
  217. }
  218.  
  219.  
  220. /**
  221. *
  222. * Name        VALIDATE_STATE - Validate the data in the "state"
  223. *                 field of the data entry screen.
  224. *
  225. * Synopsis    To be called only by the field editing functions as a
  226. *        key access routine.
  227. *
  228. * Description    This function gains control from the field editng
  229. *        functions between every poll of the keyboard.  It
  230. *        checks to see if a key has been pressed, and if so,
  231. *        it determines whether that key is a transmit key
  232. *        (either Enter or F10).    If the key is a transmit
  233. *        key, it checks to see whether the data in the
  234. *        edit buffer (a pointer to which is in the pdata
  235. *        field of pkey_data) is valid.  If it is not valid,
  236. *        it displays a message to that effect; the message is
  237. *        removed on the next keystroke.
  238. *
  239. * Returns    nothing.
  240. *
  241. **/
  242.  
  243. void state_validation(pkey_data)
  244. KB_DATA *pkey_data;
  245. {
  246.     static int     message_displayed = 0;
  247.     static char *pmessage = "Please enter a west coast state.";
  248.     static char *legal_states[] = {"AZ", "CA", "OR", "WA", "NV"};
  249.     char    *pbuffer;
  250.     int      found;
  251.     int      current_state;
  252.     int      num_states =
  253.              sizeof(legal_states) / sizeof(legal_states[0]);
  254.  
  255.     if (pkey_data->key_found)
  256.     {
  257.     if (message_displayed)
  258.     {
  259.         wnscrblk(pwin, 7, 0, 7, strlen(pmessage) - 1,
  260.              -1, -1, WNSCR_UP, 0, WN_UPDATE);
  261.         message_displayed = 0;
  262.     }
  263.  
  264.         /* Check to see if the key was an Enter or F10.     */
  265.     if (((pkey_data->key_seq.character_code == KB_C_N_ENTER) &&
  266.          (pkey_data->key_seq.key_code == KB_S_N_ENTER))     ||
  267.         ((pkey_data->key_seq.character_code == KB_C_N_F10)     &&
  268.          (pkey_data->key_seq.key_code == KB_S_N_F10)))
  269.     {
  270.         found = 0;
  271.         num_states = sizeof(legal_states) / sizeof(legal_states[0]);
  272.         pbuffer = ((ED_BUFFER *)
  273.                pkey_data->pfunction_data)->pbuffer;
  274.  
  275.         /* Validate the data.                    */
  276.         for (current_state = 0; (current_state < num_states) &&
  277.          !found; current_state++)
  278.         {
  279.         if (!strnicmp(pbuffer, legal_states[current_state],
  280.             strlen(legal_states[current_state])))
  281.         {
  282.             found = 1;
  283.         }
  284.         }
  285.  
  286.         /* If the data was not valid, display a message and */
  287.         /* ignore the transmission key.             */
  288.         if (!found)
  289.         {
  290.         wnwrrect(pwin, 7, 0, 7, strlen(pmessage) - 1,
  291.              pmessage, -1, -1, CHARS_ONLY);
  292.         scttywrt('\a', 0);
  293.         message_displayed = 1;
  294.         pkey_data->key_found = 0;
  295.         }
  296.     }
  297.     }
  298. }
  299.  
  300.  
  301. /**
  302. *
  303. * Name        VALIDATE_ZIP - Validate the data in the "zip"
  304. *                   field of the data entry screen.
  305. *
  306. * Synopsis    To be called only by the field editing functions as a
  307. *        key access routine.
  308. *
  309. * Description    This function gains control from the field editng
  310. *        functions between every poll of the keyboard.  It
  311. *        checks to see if a key has been pressed, and if so,
  312. *        it determines whether that key is an ASCII number
  313. *        (i.e., '0' through '9'). If the key not, the speaker
  314. *        is beeped to indicate to the user that they have entered
  315. *        invalid data.
  316. *
  317. * Returns    nothing.
  318. *
  319. **/
  320.  
  321. void zip_validation(pkey_data)
  322. KB_DATA *pkey_data;
  323. {
  324.     if (pkey_data->key_found)
  325.     {
  326.         /* Check to see if the key was Enter, F10, or ESC.  */
  327.     if (!(((pkey_data->key_seq.character_code == KB_C_N_ENTER) &&
  328.            (pkey_data->key_seq.key_code == KB_S_N_ENTER))       ||
  329.           ((pkey_data->key_seq.character_code == KB_C_N_F10)   &&
  330.            (pkey_data->key_seq.key_code == KB_S_N_F10))       ||
  331.           ((pkey_data->key_seq.character_code == KB_C_N_ESC)   &&
  332.            (pkey_data->key_seq.key_code == KB_S_N_ESC))))
  333.     {
  334.         /* Check to see if the key was a number.        */
  335.         if (utrange(pkey_data->key_seq.character_code,
  336.             KB_C_N_0, KB_C_N_9)           ||
  337.         utrange(pkey_data->key_seq.key_code,
  338.             KB_S_N_1, KB_S_N_0))
  339.         {
  340.         /* Key is non-numeric, so beep and swallow the key. */
  341.         scttywrt('\a', 0);
  342.         pkey_data->key_found = 0;
  343.         }
  344.     }
  345.     }
  346. }
  347.