home *** CD-ROM | disk | FTP | other *** search
- /**
- * ENTRYEDT.C Sample use of multiple window oriented edit
- * fields.
- *
- * This program displays a window which contains several edit fields
- * which comprise a simple customer information record, and requests
- * input from the user. The "state" field has a key access routine
- * associated with it to ensure that the user only enters certain
- * state names; the "zip" field has a key access routine which
- * ensures that the user only enters digits. The user transmits
- * each field (and goes on to the next) by pressing Enter; the screen
- * is transmitted when the user presses f10.
- *
- * The command line format is as follows:
- *
- * entryedt
- *
- * The purpose of ENTRYEDT is to provide a working example of the
- * proper method of the use of multiple edit fields in a window.
- *
- * Version 6.00 (C)Copyright Blaise Computing Inc. 1989
- *
- **/
-
- #include <bedit.h>
- #include <bwindow.h>
- #include <bstrings.h>
- #include <bkeys.h>
- #include <stdio.h>
-
-
- /* Internal function prototypes. */
- void main(void);
- void state_validation(KB_DATA *);
- void zip_validation(KB_DATA *);
-
- /* These constants define the number of columns */
- /* each field will have when it is displayed on the */
- /* screen. */
- #define NAME_WIDTH 40
- #define ADDRESS_WIDTH 40
- #define CITY_WIDTH 20
- #define STATE_WIDTH 2
- #define ZIP_WIDTH 5
-
- /* Allocate storage for character buffers in which */
- /* the data for the fields will be returned. */
- char name[NAME_WIDTH + 1] = "";
- char address[ADDRESS_WIDTH + 1] = "";
- char city[CITY_WIDTH + 1] = "";
- char state[STATE_WIDTH + 1] = "";
- char zip[ZIP_WIDTH + 1] = "";
-
-
- typedef struct /* FIELD_DESCRIPTOR structure: */
- { /* field info and data buffer. */
- ED_CONTROL control; /* Field control structure. */
- int buffer_size; /* Size of pbuffer in bytes. */
- char *pbuffer; /* Buffer in which to return data. */
- } FIELD_DESCRIPTOR;
-
- /* screen_fields is an array of FIELD_DESCRIPTOR */
- /* structures, each of which describes one field on */
- /* the screen. */
- FIELD_DESCRIPTOR screen_fields[] =
- {
- {{{1, 14}, {1, NAME_WIDTH}, 0x74, /* Name field. */
- {6, 7}, {0, 7}, 0, 0},
- sizeof(name), name},
- {{{2, 14}, {1, ADDRESS_WIDTH}, 0x74, /* Address field. */
- {6, 7}, {0, 7}, 0, 0},
- sizeof(address), address},
- {{{3, 14}, {1, CITY_WIDTH}, 0x74, /* City field. */
- {6, 7}, {0, 7}, 0, 0},
- sizeof(city), city},
- {{{4, 14}, {1, STATE_WIDTH}, 0x74, /* State field. */
- {6, 7}, {0, 7}, state_validation, TOUP},
- sizeof(state), state},
- {{{5, 14}, {1, ZIP_WIDTH}, 0x74, /* Zip field. */
- {6, 7}, {0, 7}, zip_validation, 0},
- sizeof(zip), zip}
- };
-
-
- typedef struct /* FIELD_LABEL structure: location & text */
- { /* of one screen field label. */
- int row; /* The row at which to place the text. */
- int col; /* The column at which to place the text. */
- char *ptext; /* The text to use as the field label. */
- } FIELD_LABEL;
-
- /* field_labels is an array of FIELD_LABEL */
- /* structures, each of which contains the location */
- /* and text for one the label for one field. */
- FIELD_LABEL field_labels[] =
- {
- {1, 8, "Name:"},
- {2, 5, "Address:"},
- {3, 8, "City:"},
- {4, 7, "State: (AZ, CA, OR, WA or NV only)"},
- {5, 9, "ZIP:"}
- };
-
- BWINDOW *pwin;
-
-
- void main()
- {
- int error_code;
- BORDER bord;
- WHERE location;
- int mode,columns,act_page;
- int cursor_was_off,row,col,high,low;
- KEY_SEQUENCE key_sequence;
- ED_ACTION action;
- ED_ACTION_LIST key_action_list;
- int index;
-
- /* First, create the window and write the field */
- /* labels to their appropriate locations. */
- pwin = wncreate(8, 60, SC_CYAN);
- if (pwin == NIL)
- exit(-1);
-
- wnselect(pwin);
-
- for (index = 0;
- index < sizeof(field_labels) / sizeof(field_labels[0]);
- index++)
- {
- if (wnwrbuf(field_labels[index].row,
- field_labels[index].col, 0,
- field_labels[index].ptext,
- -1, -1, CHARS_ONLY) == 0)
- {
- printf("Error %d writing to window.\n", b_wnerr);
- exit(-1);
- }
- }
-
- /* Install the F10 key as a transmission key, which */
- /* will be used as a signal that the user wishes to */
- /* transmit the screen. */
- key_sequence.character_code = KB_C_N_F10;
- key_sequence.key_code = KB_S_N_F10;
- key_action_list.num_actions = 1;
- action = ED_TRANSMIT;
- key_action_list.pactions = &action;
-
- if (ED_NO_ERROR != edchgkey(&key_sequence, &key_action_list))
- {
- printf("Error %d adding keystroke.\n", b_wnerr);
- exit(-1);
- }
-
- /* Now set up the BORDER and WHERE structures. */
- bord.type = BBRD_SSSS | BBRD_TCT;
- bord.attr = SC_MAGENTA;
- bord.ttattr = SC_CYAN;
- bord.pttitle = " Press f10 to transmit... ";
-
- location.dev = scmode(&mode,&columns,&act_page);
- location.page = act_page;
- location.corner.row = 10;
- location.corner.col = 5;
-
- /* Remember the cursor size and state to restore */
- /* later, and display the window. */
- cursor_was_off = sccurst(&row,&col,&high,&low);
-
- if (NIL == wndsplay(pwin, &location, &bord))
- {
- printf("Error %d displaying window.\n", b_wnerr);
- exit(-1);
- }
-
- /* Now read the fields on the screen in sequence */
- /* until the user presses F10. */
- index = 0;
- do
- {
- error_code = wnfield(pwin,
- screen_fields[index].pbuffer,
- screen_fields[index].pbuffer,
- screen_fields[index].buffer_size,
- &(screen_fields[index].control),
- &key_sequence);
- index++;
- if (index >= (sizeof(screen_fields) / sizeof(screen_fields[0])))
- index = 0;
- }
- while ((key_sequence.character_code != KB_C_N_F10) ||
- (key_sequence.key_code != KB_S_N_F10));
-
- if (error_code != WN_NO_ERROR)
- {
- printf("Error %d editing in window.\n", error_code);
- exit(-1);
- }
-
- wnremove(pwin);
- wndstroy(pwin);
- if (b_wnerr)
- {
- printf("Error %d removing or destroying window.\n", b_wnerr);
- exit(-1);
- }
-
- sccurset(row, col);
- scpgcur(cursor_was_off, high, low, CUR_NO_ADJUST);
-
- printf("Name: \"%s\"\n", name);
- printf("Address: \"%s\"\n", address);
- printf("City: \"%s\"\n", city);
- printf("State: \"%s\"\n", state);
- printf("Zip: \"%s\"\n", zip);
- }
-
-
- /**
- *
- * Name VALIDATE_STATE - Validate the data in the "state"
- * field of the data entry screen.
- *
- * Synopsis To be called only by the field editing functions as a
- * key access routine.
- *
- * Description This function gains control from the field editng
- * functions between every poll of the keyboard. It
- * checks to see if a key has been pressed, and if so,
- * it determines whether that key is a transmit key
- * (either Enter or F10). If the key is a transmit
- * key, it checks to see whether the data in the
- * edit buffer (a pointer to which is in the pdata
- * field of pkey_data) is valid. If it is not valid,
- * it displays a message to that effect; the message is
- * removed on the next keystroke.
- *
- * Returns nothing.
- *
- **/
-
- void state_validation(pkey_data)
- KB_DATA *pkey_data;
- {
- static int message_displayed = 0;
- static char *pmessage = "Please enter a west coast state.";
- static char *legal_states[] = {"AZ", "CA", "OR", "WA", "NV"};
- char *pbuffer;
- int found;
- int current_state;
- int num_states =
- sizeof(legal_states) / sizeof(legal_states[0]);
-
- if (pkey_data->key_found)
- {
- if (message_displayed)
- {
- wnscrblk(pwin, 7, 0, 7, strlen(pmessage) - 1,
- -1, -1, WNSCR_UP, 0, WN_UPDATE);
- message_displayed = 0;
- }
-
- /* Check to see if the key was an Enter or F10. */
- if (((pkey_data->key_seq.character_code == KB_C_N_ENTER) &&
- (pkey_data->key_seq.key_code == KB_S_N_ENTER)) ||
- ((pkey_data->key_seq.character_code == KB_C_N_F10) &&
- (pkey_data->key_seq.key_code == KB_S_N_F10)))
- {
- found = 0;
- num_states = sizeof(legal_states) / sizeof(legal_states[0]);
- pbuffer = ((ED_BUFFER *)
- pkey_data->pfunction_data)->pbuffer;
-
- /* Validate the data. */
- for (current_state = 0; (current_state < num_states) &&
- !found; current_state++)
- {
- if (!strnicmp(pbuffer, legal_states[current_state],
- strlen(legal_states[current_state])))
- {
- found = 1;
- }
- }
-
- /* If the data was not valid, display a message and */
- /* ignore the transmission key. */
- if (!found)
- {
- wnwrrect(pwin, 7, 0, 7, strlen(pmessage) - 1,
- pmessage, -1, -1, CHARS_ONLY);
- scttywrt('\a', 0);
- message_displayed = 1;
- pkey_data->key_found = 0;
- }
- }
- }
- }
-
-
- /**
- *
- * Name VALIDATE_ZIP - Validate the data in the "zip"
- * field of the data entry screen.
- *
- * Synopsis To be called only by the field editing functions as a
- * key access routine.
- *
- * Description This function gains control from the field editng
- * functions between every poll of the keyboard. It
- * checks to see if a key has been pressed, and if so,
- * it determines whether that key is an ASCII number
- * (i.e., '0' through '9'). If the key not, the speaker
- * is beeped to indicate to the user that they have entered
- * invalid data.
- *
- * Returns nothing.
- *
- **/
-
- void zip_validation(pkey_data)
- KB_DATA *pkey_data;
- {
- if (pkey_data->key_found)
- {
- /* Check to see if the key was Enter, F10, or ESC. */
- if (!(((pkey_data->key_seq.character_code == KB_C_N_ENTER) &&
- (pkey_data->key_seq.key_code == KB_S_N_ENTER)) ||
- ((pkey_data->key_seq.character_code == KB_C_N_F10) &&
- (pkey_data->key_seq.key_code == KB_S_N_F10)) ||
- ((pkey_data->key_seq.character_code == KB_C_N_ESC) &&
- (pkey_data->key_seq.key_code == KB_S_N_ESC))))
- {
- /* Check to see if the key was a number. */
- if (utrange(pkey_data->key_seq.character_code,
- KB_C_N_0, KB_C_N_9) ||
- utrange(pkey_data->key_seq.key_code,
- KB_S_N_1, KB_S_N_0))
- {
- /* Key is non-numeric, so beep and swallow the key. */
- scttywrt('\a', 0);
- pkey_data->key_found = 0;
- }
- }
- }
- }