IDENTIFICATION DIVISION.
PROGRAM-ID. SD002070.
*>
*> Warren W. Gay
*>
*> THIS IS THE GENERATION SCREEN MODULE. THIS SCREEN PERMITS THE USER TO
*> PERFORM AN INTERACTIVE CODE GENERATION, ALLOWING SELECTIVE OPTIONS
*> FOR WORKING-STORAGE SECTION CODE, PROCEDURE DIVISION CODE, AND/OR
*> SCREEN IMAGE TEXT FILE.
*>
*> INPUTS :
*> 
*>     NC-COBCURSES            COBCURSES GLOBAL & WORK AREAS
*>     LS-SCREEN-NAME          THE SCREEN NAME TO GENERATE FOR
*>
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

    SELECT SCREEN-FILE
        ASSIGN TO SCREEN-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCN-NAME.

DATA DIVISION.
FILE SECTION.

    FD  SCREEN-FILE.
    01  SCREEN-RECORD.
        COPY SCREEN-01.

WORKING-STORAGE SECTION.

    COPY COBCRETC.
    COPY COBCATTR.
    COPY COBCCOLOUR.
    COPY COBCURSL.

    COPY SD002070-WS.

    01  FILE-NAMES.
        10  FILE-NAME-LENGTH                PIC 9999.
        10  SCREEN-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/SCREENS.X".

    01  WS-CODE-GENERATOR.
        10  WS-CODE-FILENAME                PIC X(16).      *> WORKING-STORAGE SECTION CODE FILE
        10  PD-CODE-FILENAME                PIC X(16).      *> PROCEDURE DIVISION CODE FILE
        10  SI-TEXT-FILENAME                PIC X(16).      *> SCREEN IMAGE FILE

    01  WS-DIRECTORIES.
        10  WS-COPY-BOOK-DIR                PIC X(512).     *> COPY BOOK DIRECTORY
        10  WS-SCREEN-IMAGE-DIR             PIC X(512).     *> SCREEN IMAGE DIRECTORY

    01  COUNT-SEGMENTS                      PIC 9999.
    01  COUNT-FIELDS                        PIC 9999.
    01  COUNT-STATES                        PIC 9999.
    01  COUNT-MENUS                         PIC 9999.
    01  COUNT-ITEMS                         PIC 9999.
    01  SCREEN-DESCRIPTION                  PIC X(50).

    01  WS-FLAGS.
        10  WS-SCREEN-TOO-SMALL-FLAG    PIC X VALUE 'N'.
            88  WS-SCREEN-TOO-SMALL     VALUE 'Y' FALSE IS 'N'.

LINKAGE SECTION.

    COPY COBCURSG.

    01  LS-SCREEN-NAME              PIC X(16).

PROCEDURE DIVISION
    USING NC-COBCURSES, LS-SCREEN-NAME.

MAIN-PROGRAM.
    PERFORM 1000-INITIALIZATION.
    IF NOT WS-SCREEN-TOO-SMALL THEN
        PERFORM 5000-PROCESS
    END-IF.
    PERFORM 9000-FINALIZE.
    EXIT PROGRAM.

1000-INITIALIZATION.
    MOVE SPACES TO WS-COPY-BOOK-DIR.
    MOVE SPACES TO WS-SCREEN-IMAGE-DIR.

    MOVE FNO-ACTION TO NC-FSEQ-STATE.

    MOVE LENGTH OF SCREEN-FILE-NAME TO FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING SCREEN-FILE-NAME, FILE-NAME-LENGTH.

    OPEN INPUT SCREEN-FILE.

    PERFORM 1020-COBCURSES-INIT.
    MOVE FNO-ACTION TO NC-FSEQ-STATE.
    EXIT.

1020-COBCURSES-INIT.
    PERFORM NC-INIT.
    PERFORM NC-CLEAR.
    MOVE LS-SCREEN-NAME TO FLD-SCREEN-NAME.
    PERFORM 1100-LOOKUP-SCREEN.
    PERFORM 1030-SCREEN-INIT.
    EXIT.

1030-SCREEN-INIT.
    COPY SD002070-PD.
    PERFORM 1040-DRAW-SCREEN.
    IF RETURN-CODE NOT = NC-RET-OK THEN
        SET WS-SCREEN-TOO-SMALL TO TRUE
    END-IF.
    EXIT.

1040-DRAW-SCREEN.
    PERFORM NC-DRAW-SCREEN.
    PERFORM NC-DRAW-FIELDS.
    EXIT.

1100-LOOKUP-SCREEN.
    MOVE LS-SCREEN-NAME TO SCN-NAME.
    READ SCREEN-FILE
        INVALID KEY
            MOVE "Error: Record not found!?!?" TO FLD-SCREEN-DESCRIPTION
            MOVE SPACES TO FLD-WS-SECTION-CODE-FILENAME, FLD-GENERATE-PD-CODE-FILENAME
        NOT INVALID KEY
            MOVE SCN-DESCRIPTION TO FLD-SCREEN-DESCRIPTION
            MOVE SCN-WS-SECTION TO FLD-WS-SECTION-CODE-FILENAME
            MOVE SCN-PROCEDURE-DIVISION TO FLD-GENERATE-PD-CODE-FILENAME
    END-READ.

    IF FLD-WS-SECTION-CODE-FILENAME NOT = SPACES THEN
        MOVE 'Y' TO FLD-GENERATE-WS-CODE-FLAG
    ELSE
        MOVE 'N' TO FLD-GENERATE-WS-CODE-FLAG
    END-IF.

    IF FLD-GENERATE-PD-CODE-FILENAME NOT = SPACES THEN
        MOVE 'Y' TO FLD-GENERATE-PD-FLAG
    ELSE
        MOVE 'N' TO FLD-GENERATE-PD-FLAG
    END-IF.

    MOVE LS-SCREEN-NAME TO FLD-SCREEN-IMAGE-FILENAME.
    MOVE 'N' TO FLD-GENERATE-SCREEN-IMAGE-FLAG.
    EXIT.

5000-PROCESS.
    PERFORM NC-FIELD-STATE-MACHINE.
    EXIT.

5300-ACTION-E.
    MOVE 9999 TO NC-FSEQ-NEXT
    EXIT.

5400-ACTION-G.
    MOVE ZERO TO COUNT-SEGMENTS, COUNT-FIELDS, COUNT-STATES.

    IF FLD-GENERATE-WS-CODE-FLAG NOT = 'Y' THEN
        MOVE SPACES TO WS-CODE-FILENAME
    ELSE
        MOVE FLD-WS-SECTION-CODE-FILENAME TO WS-CODE-FILENAME
    END-IF.

    IF FLD-GENERATE-PD-FLAG NOT = 'Y' THEN
        MOVE SPACES TO PD-CODE-FILENAME
    ELSE
        MOVE FLD-GENERATE-PD-CODE-FILENAME TO PD-CODE-FILENAME
    END-IF.

    IF FLD-GENERATE-SCREEN-IMAGE-FLAG NOT = 'Y' THEN
        MOVE SPACES TO SI-TEXT-FILENAME
    ELSE
        MOVE FLD-SCREEN-IMAGE-FILENAME TO SI-TEXT-FILENAME
    END-IF.

    IF FLD-GENERATE-WS-CODE-FLAG = 'Y' OR FLD-GENERATE-PD-FLAG = 'Y' OR FLD-GENERATE-SCREEN-IMAGE-FLAG = 'Y' THEN
        PERFORM 5500-GENERATE-CODE
    ELSE
        MOVE "You have not selected any files to generate." TO NC-MSGBUF
        PERFORM NC-PUT-ERROR-CR
    END-IF.
    EXIT.

5500-GENERATE-CODE.
    CALL "libcobcurses_codegen" USING LS-SCREEN-NAME, WS-CODE-FILENAME, PD-CODE-FILENAME, SI-TEXT-FILENAME,
        WS-COPY-BOOK-DIR, WS-SCREEN-IMAGE-DIR, 
        COUNT-SEGMENTS, COUNT-FIELDS, COUNT-STATES, COUNT-MENUS, COUNT-ITEMS
        SCREEN-DESCRIPTION.

    INITIALIZE NC-MSGBUF.

    IF RETURN-CODE = NC-RET-OK THEN
        STRING COUNT-SEGMENTS, " SEGMENTS, ", COUNT-FIELDS, " FIELDS, ", COUNT-STATES, " STATES, ",
            COUNT-MENUS, " MENUS, ", COUNT-ITEMS, " ITEMS ",
            "GENERATED FOR ", LS-SCREEN-NAME
            INTO NC-MSGBUF
        PERFORM NC-PUT-MESSAGE-CR
        PERFORM 5300-ACTION-E
    ELSE
        STRING "ERROR: ", LS-SCREEN-NAME, " IS NOT ON FILE." INTO NC-MSGBUF
        PERFORM NC-PUT-ERROR-CR
    END-IF.
    EXIT.

5900-ACTION-EDIT.
    IF NC-FIELD-EDIT-TARGET NOT = 'Y' THEN
        MOVE "You cannot edit that field." TO NC-MSGBUF
        PERFORM NC-PUT-ERROR-OVERRIDE
    ELSE
        MOVE FSEQ-EDIT TO NC-FSEQ-NEXT
        MOVE NC-FIELD-SEARCH TO NC-FSEQ-FIELD-NO(NC-FSEQ-NEXT)
    END-IF.
    EXIT.

6100-UPDATE-SCREEN.
    PERFORM NC-DRAW-FIELDS.
    EXIT.

7000-ACTION-EVENT.
    IF NC-FIELD-ACTION = 'E' THEN
        PERFORM 5300-ACTION-E
        MOVE SPACES TO FLD-ACTION
    END-IF.

    IF NC-FIELD-ACTION = 'G' THEN
        PERFORM 5400-ACTION-G
        MOVE SPACES TO FLD-ACTION
    END-IF.

    IF NC-FIELD-ACTION = SPACES
        PERFORM 5900-ACTION-EDIT
        MOVE SPACES TO FLD-ACTION
    END-IF.

    IF FLD-ACTION NOT = SPACES THEN
        PERFORM 8000-ERROR-ACTION
    END-IF.
    EXIT.

7500-FIELD-EVENT.
    IF NC-FIELD-NUMBER = FNO-ACTION
        PERFORM 7000-ACTION-EVENT
    END-IF.
    EXIT.

7600-MOUSE-EVENT.
    PERFORM 5900-ACTION-EDIT.
    EXIT.

7700-STATE-CHANGE-EVENT.
    EXIT.

8000-ERROR-ACTION.
    SET NC-MSG-TEXT TO ADDRESS OF INF-ACTION.
    MOVE LENGTH OF INF-ACTION TO NC-MSG-LENGTH.
    PERFORM NC-ERROR-MESSAGE-OVERRIDE.
    EXIT.

9000-FINALIZE.
    CLOSE SCREEN-FILE.
    PERFORM NC-CLEAR.
    PERFORM NC-FINALIZE.
    EXIT.

NC-FIELD-EVENT.
    PERFORM 7500-FIELD-EVENT.
    EXIT.

NC-VERIFY-EVENT.
    EXIT.

NC-CHANGE-EVENT.
    EXIT.

NC-MOUSE-EVENT.
    PERFORM 7600-MOUSE-EVENT.
    EXIT.

NC-STATE-CHANGE-EVENT.
    PERFORM 7700-STATE-CHANGE-EVENT.
    EXIT.

NC-FKEY-EVENT.
    EXIT.

COPY COBCURSQ.

END PROGRAM SD002070.
