home *** CD-ROM | disk | FTP | other *** search
- #include "microlib.h"
- #include <malloc.h>
-
- #include "fxaddons.h"
- #define SCR_FILE SCR_NEWFORM
- #define DBF_FILE DBF_FORMS
-
- extern IDDFILE iddForms;
- static IDDFILE *idd = &iddForms;
-
- typedef struct {
- char deleted;
- char code[7];
- char name[80];
- char page[2];
- char orient[7];
- char header[7];
- char file[7];
- } REC;
-
- typedef struct __info__ {
- F4FIELD *field;
- unsigned char find_info[100];
- int compare_len;
- } FORMS_FILTER_INFO;
-
- int S4CALL filter_forms(void *);
-
- static int append(D4DATA *, REC *cr);
-
- int newform()
- {
- char *cr;
- char *pszKey1;
- char *pszKey2;
- int wref;
-
- int count, i;
-
- int seek_val;
- int read_val;
- int key_val;
-
- int bDone2;
- int nCurrKey;
-
- // size of frequently used fields
- // in every database field 1 is the access code of a record
- // while field 2 is actually the major key of the database
- int nSizeofCode = idd->Field[1].nFileSize;
- int nSizeofName = idd->Field[2].nFileSize;
- int nSizeofRec;
-
- // temp fields
- char *szTmpCode;
- char *szTmpName;
-
- D4DATA *dbf;
-
- X4FILTER filter;
- FORMS_FILTER_INFO info;
-
- // Allocate memory for my record buffer
- ComputeRecordSize(idd, nSizeofRec);
- if ((cr = (char *) malloc(nSizeofRec)) == NULL) {
- w4display(mes[MT], mes[ME+1], (char *) 0);
- w4exit(1);
- }
- pszKey1 = (char *)cr + 1;
- pszKey2 = (char *)cr + 8;
- szTmpCode = (char *) alloca(nSizeofCode + 1);
- szTmpName = (char *) alloca(nSizeofName + 1);
-
- // Define, activate and clear Data Entry screen
- wref = w4define(1, 0, 22, 79);
- w4popup();
- w4memory();
- w4border(SINGLE, custom[MAIN_N]);
- w4title(0, 1, mes[MT_NEWFORM], custom[MAIN_N]);
- w4attribute(custom[MAIN_N]);
- w4activate(wref);
- w4clear(0);
-
- // Load screen from disk and get field coordinates
- if (screen_file(screens(SCR_FILE), 22, 78))
- w4exit(1);
- if (get_input_positions(screens(SCR_FILE)))
- w4exit(1);
-
- // Open Databases and select the proper Tags...
- dbf = d4open(&set, files(DBF_FILE));
- d4tag_select(dbf, d4tag(dbf, "CODE_T"));
-
- info.field = d4field(dbf, "CODE");
- strcpy(info.find_info, "R");
- info.compare_len = 1;
- x4init(&filter, dbf, filter_forms, &info);
-
- g4attribute (custom[MAIN_I]); // Set input attribute.
-
- // Main append, scan, edit and delete loop.
- for (;;) { // ==L1==
-
- // Blank my record buffer and display it
- memset(cr, (int) ' ', sizeof(REC));
-
- DisplayFields(idd, (char *) cr);
-
- statline(mes[MS]);
- helpline(mes[MH]);
- g4delimiter("[]");
-
- // Get code or any other user input.
- read_val =0;
- while (read_val != ESC && read_val != F5 && read_val != RETURN\
- && read_val != TAB && read_val != F1) {
-
- g4(srow[1], scol[1], pszKey1);
- g4picture("R999999");
- read_val = g4read();
- }
-
- // If Function Keys
- if (read_val == ESC)
- break; // Out of ==L1==
-
- if (read_val == F1) {
- help("CASE_QUERY");
- continue;
- }
-
- if (read_val == F5) {
- append(dbf, (REC *)cr);
- if (screen_file(screens(SCR_FILE), 22, 78))
- w4exit(1);
- continue;
- }
-
- // If ENTER was pressed and CODE field is blank...
- if (!memcmp(pszKey1 + 1, space(nSizeofCode - 1), nSizeofCode - 1)) {
- statline(mes[MS+7]);
- helpline(mes[MH+22]);
- read_val = 0;
- while (read_val != RETURN && read_val != ESC && read_val != TAB) {
- g4(srow[2], scol[2], pszKey2);
- //g4width(nSizeofName, nSizeofName);
- g4width(nSizeofName, idd->Field[2].nCrtSize);
- read_val = g4read();
- if (read_val == F1)
- help("CASE_QUERY");
- }
-
- if (!memcmp(pszKey2, space(nSizeofName), nSizeofName))
- continue;
-
- d4tag_select(dbf, d4tag(dbf, "NAME"));
- memcpy(szTmpName, pszKey2, nSizeofName);
- szTmpName[ nSizeofName ] = '\0';
-
- //seek_val = d4seek(dbf, szTmpName);
- c4trim_n(szTmpName, nSizeofName + 1);
- info.field = d4field(dbf, "TITLE1");
- strcpy(info.find_info, szTmpName);
- info.compare_len = strlen(szTmpName);
-
- seek_val = x4seek(&filter, szTmpName);
- //d4tag_select(dbf, d4tag(dbf, "CODE_T"));
- d4unlock_index(dbf);
- if (seek_val == r4eof || seek_val == r4after) {
- w4display("", mes[ME+2], (char *) 0);
-
- info.field = d4field(dbf, "CODE");
- strcpy(info.find_info, "R");
- info.compare_len = 1;
-
- continue; // ==L1==
- }
- }
- else { // If CODE field is not empty...
- // Zerofill and show the entered CODE.
- zerofill(pszKey1 + 1, nSizeofCode - 1);
- w4attribute(custom[MAIN_I]);
- w4num(srow[1], scol[1], pszKey1, nSizeofCode);
- w4attribute(custom[MAIN_N]);
-
- d4tag_select(dbf, d4tag(dbf, "CODE_T"));
-
- // Copy code and add NULL.
- memcpy(szTmpCode, pszKey1, nSizeofCode);
- szTmpCode[ nSizeofCode ] = '\0';
-
- info.field = d4field(dbf, "CODE");
- strcpy(info.find_info, szTmpCode);
- info.compare_len = strlen(szTmpCode);
- //seek_val = d4seek(dbf, szTmpCode);
- seek_val = x4seek(&filter, szTmpCode);
- d4unlock_index(dbf);
- if (seek_val == r4eof || seek_val == r4after) {
- w4display("", mes[ME+2], (char *) 0);
- continue; // ==L1==
- }
- }
-
- // If CODE or SURNAMEKEY was found display the record and let the
- // user to skip through the records, edit, delete, change to
- // the second screen or ask for help.
- // ?
- memcpy(cr, (char *) d4record(dbf), sizeof(REC));
-
- helpline(mes[MH+33]); // Show new available keys.
- statline(mes[MS+8]);
-
- // Clear delimiters from CODE and SURNAME field
- // w4num(srow[1], scol[1] -1, space(nSizeofCode +2), nSizeofCode +2);
- // w4num(srow[2], scol[2] -1, space(nSizeofName +2), nSizeofName +2);
-
- DisplayFields(idd, (char *)cr);
-
- bDone2 = FALSE;
- while (!bDone2) {
- nCurrKey = _kbdhit();
- switch (nCurrKey) {
-
- case VK_PGUP :
- if (d4skip(dbf, -1L) == r4bof)
- d4bottom(dbf);
- break;
-
- case VK_PGDN :
- if (d4skip(dbf, 1L) == r4eof)
- d4top(dbf);
- break;
-
- case VK_ESCAPE :
- bDone2 = TRUE;
- continue;
- //break; // ==L2 ==
-
- case VK_F2 :
- nCurrKey = EditRecord(dbf, idd, (char *) cr);
- if (screen_file(screens(SCR_FILE), 22, 78))
- w4exit(1);
- if (nCurrKey == VK_ESCAPE) {
- bDone2 = TRUE;
- continue;
- }
- DisplayFields(idd, (char *) cr);
- continue;
-
- //case VK_F8 :
- // delete(dbf, filter);
- // break;
-
- default :
- continue;
- }
-
- memcpy(cr, (char *) d4record(dbf), sizeof(REC));
- DisplayFields(idd, (char *) cr);
-
- }
- } // ==L1==
-
- statline(mes[MS+2]);
- helpline(mes[MH+3]);
- d4close_all(&set);
-
- free(cr);
- w4deactivate(wref);
- w4close(wref);
-
- // Return the menu item to be selected.
- return (2);
- }
-
- /************************************************************************/
- /* Append a record */
- /************************************************************************/
- static int append(dbfile, cr)
- D4DATA *dbfile;
- REC *cr;
- {
- REC *rec_buffer;
- D4DATA *dbcodes; /* File to get last code. */
- int count; /* Counter. */
- int read_val; /* Returned value. */
-
- char old_code[7];
- char tmp_code[7]; /* Temporary storage of CODE field */
- /* because when I clear my buffer I lose */
- /* the next available code. */
- int nSizeofCode; /* Length of frequently used fields ... */
- int nSizeofName;
- int someone_used_the_code;
-
- someone_used_the_code = FALSE;
- nSizeofCode = sizeof(cr->code);
-
- d4tag_select(dbfile , d4tag(dbfile, "CODE_T"));
-
- /* Main Append Loop (where next code number is given etc.). */
- for (;;) { /* ==L1== */
- /* Read the last code used from CODES file. */
- dbcodes = d4open(&set, files(DBF_CODES));
- d4tag_select(dbcodes, d4tag(dbcodes, "CODE_T"));
- d4lock_file(dbcodes);
- d4lock_index(dbcodes);
- if (d4seek(dbcodes, "R") != 0) {
- d4close_all(&set);
- w4display(mes[MT+1], mes[ME+9], (char *) 0);
- w4exit(1);
- }
- memcpy(cr->code, f4str(d4field(dbcodes, "CODE")), nSizeofCode);
- memcpy(old_code, cr->code, nSizeofCode);
- old_code[7]='\0';
- d4close(dbcodes);
-
- /* The character field is converted to numeric , */
- /* the value is increased by 1 and then the result */
- /* is converted again to character. */
- /* At the numeric-->character conversion I use a */
- /* negative length in order to "zerofill" the result. */
- c4ltoa45(c4atol(cr->code+1, nSizeofCode-1) + 1, cr->code+1, -6);
-
- /* Save the next available code. */
- memcpy(tmp_code, cr->code, nSizeofCode);
- tmp_code[7]='\0';
-
- /* Show next available code, clear buffer, restore the next */
- /* available code. Show the screen file and prepare gets. */
-
- w4attribute(custom[MAIN_I]);
- w4num(srow[1], scol[1], cr->code, nSizeofCode);
- w4attribute(custom[MAIN_N]);
- memset(cr, (int) ' ', sizeof(REC));
- memcpy(cr->code, tmp_code, nSizeofCode);
-
- if (get_input_positions(screens(SCR_FILE)))
- w4exit(1);
- read_val = EditFields(idd, (char *)cr);
-
- /* Entry Loop */
- for (;;) {
- helpline(mes[MH+4]);
- statline(mes[MS+4]);
- //read_val = g4read();
- switch (read_val) {
- case ESC :
- if (warning(mes[MH+6]))
- return(0);
- break;
-
- case F1 :
- help("CASE_APPEND");
- break;
-
- case F10 :
- dbcodes = d4open(&set, files(DBF_CODES));
- d4tag_select(dbcodes, d4tag(dbcodes, "CODE_T"));
- d4lock_file(dbcodes);
- d4lock_index(dbcodes);
- if(d4seek(dbcodes, old_code) != 0) {
- if(d4seek(dbcodes, "R") != 0) {
- d4close_all(&set);
- w4display(mes[MT+1], mes[ME+9], (char *) 0);
- w4exit(1);
- }
- memcpy(cr->code, f4str(d4field(dbcodes, "CODE")), nSizeofCode);
- c4ltoa45(c4atol(cr->code+1, nSizeofCode-1) + 1, cr->code+1, -6);
- memcpy(tmp_code, cr->code, nSizeofCode);
- tmp_code[7]='\0';
- f4assign(d4field(dbcodes, "CODE"), tmp_code);
- d4close(dbcodes);
- someone_used_the_code = TRUE;
- }
- else { // If CODE is still the same.
- f4assign(d4field(dbcodes, "CODE"), tmp_code);
- d4close(dbcodes);
- }
-
- //for(count = 0; count <= nSizeofName -1; count ++)
- // cr->lname[count] = gtoupper(cr->lname[count]);
- rec_buffer = (REC *) d4record(dbfile);
- d4append_start(dbfile, 0);
- d4blank(dbfile);
- memcpy(rec_buffer, cr, sizeof(REC));
- d4append(dbfile);
- d4flush_all(dbfile);
- d4unlock_all_data(dbfile);
- d4unlock_index(dbfile);
- if (someone_used_the_code) {
- w4display("", mes[ME+10], mes[ME+11], tmp_code, (char *) 0);
- someone_used_the_code = FALSE;
- }
- if (screen_file(screens(SCR_FILE), 19, 78))
- w4exit(1);
- return (1);
- default :
- EditFields(idd, (char *)cr);
- break;
- }
- }
- } /* ==L1== */
- }
-
- int S4CALL filter_forms(void *info_parm)
- {
- FORMS_FILTER_INFO *info;
-
- info = (FORMS_FILTER_INFO *) info_parm;
-
- if (memcmp(f4ptr(info->field), info->find_info, info->compare_len))
- return (r4ignore);
-
- return (r4keep);
- }
-