REPORT ZPOPUPDE.
************************************************************************
* The programs ZPOPUP and ZPOPUPDE implement a talk utility -
* - similar to the UNIX talk or write commands.
* The two instances of zpopups will negotiate the communication
* parameters and call zpopupde, the terminal module. Using the
* terminal window's buttons you can chat, get help or exit.
************************************************************************
TABLES: USR02, SSCRFIELDS, INDX.
DATA: C12(12), Q(12), LINE(90).
DATA: FIRST_TIME VALUE '1'.
DATA: I_AM.
DATA: A_NAME(22), B_NAME(22), AB_NAME(22), BA_NAME(22), MY_NAME(22).
DATA: MY_TURN.
DATA: MESSAGE(30) VALUE '   Waiting for an answer'.
DATA: NO_HELP_SCROLL.
* Selection screen begin
INITIALIZATION.
* Receive area
  SET PF-STATUS 'ZCLEAR'.
  SELECTION-SCREEN BEGIN OF BLOCK A WITH FRAME.
  SELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN POSITION 1.
  PARAMETERS: REMOTE(12) MODIF ID ABB.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: A(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: B(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: C(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: D(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: E(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: F(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
* Send area
  SELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN POSITION 1.
  PARAMETERS: LOCAL(12) MODIF ID ABB.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: G(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: H(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: I(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: J(45) MODIF ID ABC LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: K(45) MODIF ID ABD LOWER CASE.
  SELECTION-SCREEN END OF LINE.
  selection-screen begin of line.selection-screen position 1.
  PARAMETERS: L(45) MODIF ID ABD LOWER CASE.
  SELECTION-SCREEN END OF LINE.
* Buttons
  SELECTION-SCREEN SKIP 2.
  SELECTION-SCREEN PUSHBUTTON 1(13) TEXT-010 USER-COMMAND SND.
  SELECTION-SCREEN PUSHBUTTON 17(13) TEXT-011 USER-COMMAND EXI.
  SELECTION-SCREEN PUSHBUTTON 33(13) TEXT-012 USER-COMMAND HLP.
  SELECTION-SCREEN END OF BLOCK A.
* End of selection screen

* When a button is pressed
AT SELECTION-SCREEN.
* The SEND button or ENTER was pressed
  IF SSCRFIELDS-UCOMM = 'SND' OR SSCRFIELDS-UCOMM IS INITIAL
     OR SSCRFIELDS-UCOMM = 'AAAA'.
    IF I_AM = 'I'.        " I am an initiator
      LINE = K. LINE+45(45) = L.
      EXPORT LINE TO DATABASE INDX(ST) ID AB_NAME.
      COMMIT WORK AND WAIT.
      MY_TURN = '0'.
    ELSEIF I_AM = 'R'.    " I am a responder
      LINE = K. LINE+45(45) = L.
      EXPORT LINE TO DATABASE INDX(ST) ID BA_NAME.
      COMMIT WORK AND WAIT.
      MY_TURN = '0'.
    ENDIF.
* The exit button was pressed (or green or yellow arrow, or red cross)
  ELSEIF SSCRFIELDS-UCOMM = 'EXI'
      OR SSCRFIELDS-UCOMM = 'BBBB'
      OR SSCRFIELDS-UCOMM = 'CCCC'
      OR SSCRFIELDS-UCOMM = 'DDDD'.
* Send the exit message to the partner and exit.
    LINE = 'EXITEXITEXITEXITEXIT'.
    EXPORT LINE TO DATABASE INDX(ST) ID BA_NAME.
    EXPORT LINE TO DATABASE INDX(ST) ID AB_NAME.
    COMMIT WORK AND WAIT.
    LEAVE PROGRAM.
* If the HELP button was pressed
  ELSEIF SSCRFIELDS-UCOMM = 'HLP'.
    NO_HELP_SCROLL = '1'.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
         TITEL = 'HELP'
         TXT1  = 'Enter the message using the input lines at the bottom'
         TXT2  = 'Press ENTER or the SEND button to send the message'
         TXT3  = 'Wait for the answer'
         TXT4  = 'Press EXIT to close the connection'.
  ENDIF.
* End: when a button is pressed

* Prepare the selection screen for output
AT SELECTION-SCREEN OUTPUT.
* When the display module runs the first time: import the communication
* parameters
  IF FIRST_TIME = 1.
    IMPORT I_AM A_NAME B_NAME AB_NAME BA_NAME FROM MEMORY ID 'AAA'.
    IF I_AM = 'I'.
      LOCAL = A_NAME.
      REMOTE = B_NAME.
      MY_TURN = '1'.
    ELSEIF I_AM = 'R'.
      LOCAL = B_NAME.
      REMOTE = A_NAME.
      MY_TURN = '0'.
    ENDIF.
    FIRST_TIME = 0.
  ENDIF.                               " First time
* Wait for an answer (my_turn = '0')
  IF MY_TURN = '0'.                    " Wait for the partner
    CLEAR LINE.
    IF I_AM = 'I'.                     " Listen on BA
      DO.
        IMPORT LINE FROM DATABASE INDX(ST) ID BA_NAME.
* Horray, a message has arrived
        IF NOT LINE IS INITIAL.
          DELETE FROM INDX WHERE RELID = 'ST' AND
                                 SRTFD = BA_NAME.
          COMMIT WORK AND WAIT.
          MY_TURN = '1'.
          EXIT.
        ENDIF.
* No message yet: display a progress indicator
        SHIFT MESSAGE CIRCULAR RIGHT.
        CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
             EXPORTING
                  PERCENTAGE = ''
                  TEXT       = MESSAGE.
      ENDDO.
* Wait for an answer (my_turn = '0')
    ELSEIF I_AM = 'R'.                 " Listen on AB
      DO.
        IMPORT LINE FROM DATABASE INDX(ST) ID AB_NAME.
* Horray, a message has arrived
        IF NOT LINE IS INITIAL.        " An answer came
          DELETE FROM INDX WHERE RELID = 'ST' AND
                                 SRTFD = AB_NAME.
          COMMIT WORK AND WAIT.
          MY_TURN = '1'.
          EXIT.
        ENDIF.
* No message yet: display a progress indicator
        SHIFT MESSAGE CIRCULAR RIGHT.
        CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
             EXPORTING
                  PERCENTAGE = ''
                  TEXT       = MESSAGE.
      ENDDO.
    ENDIF.
  ENDIF.
* my_turn = '1'
* An exit message come: free up the communication area and exit
  IF LINE = 'EXITEXITEXITEXITEXIT'.
    DELETE FROM INDX WHERE RELID = 'ST' AND
                           SRTFD = AB_NAME.
    DELETE FROM INDX WHERE RELID = 'ST' AND
                           SRTFD = BA_NAME.
    COMMIT WORK AND WAIT.
    CALL FUNCTION 'POPUP_TO_INFORM'
         EXPORTING
              TITEL = 'INFORMATION'
              TXT1  = 'Partner closed the connection'
              TXT2  = ''.
    LEAVE PROGRAM.
  ENDIF.
* Prepare the selection screen fields for output
  LOOP AT SCREEN.
    IF SCREEN-GROUP1 = 'ABC' OR SCREEN-GROUP1 = 'ABB'.
      SCREEN-INPUT = '0'.
      MODIFY SCREEN.
    ENDIF.
    IF SCREEN-GROUP1 = 'ABB'.
      SCREEN-INTENSIFIED = '1'.
      MODIFY SCREEN.
    ENDIF.
  ENDLOOP.
* Scroll the input and output lines
  IF NO_HELP_SCROLL <> '1'.
    G = I. H = J.
    I = K. J = L.
    CLEAR K. CLEAR L. CLEAR NO_HELP_SCROLL.
  ENDIF.
  IF NOT LINE IS INITIAL.              " Refresh the receive lines.
    A = C. B = D.
    C = E. D = F.
    E = LINE(45). F = LINE+45(45).
  ENDIF.